Skip to content

Commit d994fb1

Browse files
authored
Fix RAC progressbar and meter overflow (#5706)
* Fix RAC progressbar and meter overflow
1 parent 4ae2831 commit d994fb1

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

packages/react-aria-components/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@react-aria/toolbar": "3.0.0-beta.1",
4545
"@react-aria/utils": "^3.23.0",
4646
"@react-stately/table": "^3.11.4",
47+
"@react-stately/utils": "^3.9.0",
4748
"@react-types/form": "^3.7.1",
4849
"@react-types/grid": "^3.2.3",
4950
"@react-types/shared": "^3.22.0",

packages/react-aria-components/src/Meter.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import {AriaMeterProps, useMeter} from 'react-aria';
14+
import {clamp} from '@react-stately/utils';
1415
import {ContextValue, forwardRefType, RenderProps, SlotProps, useContextProps, useRenderProps, useSlot} from './utils';
1516
import {LabelContext} from './Label';
1617
import React, {createContext, ForwardedRef, forwardRef} from 'react';
@@ -38,6 +39,7 @@ function Meter(props: MeterProps, ref: ForwardedRef<HTMLDivElement>) {
3839
minValue = 0,
3940
maxValue = 100
4041
} = props;
42+
value = clamp(value, minValue, maxValue);
4143

4244
let [labelRef, label] = useSlot();
4345
let {

packages/react-aria-components/src/ProgressBar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import {AriaProgressBarProps, useProgressBar} from 'react-aria';
14+
import {clamp} from '@react-stately/utils';
1415
import {ContextValue, RenderProps, SlotProps, useContextProps, useRenderProps, useSlot} from './utils';
1516
import {LabelContext} from './Label';
1617
import React, {createContext, ForwardedRef, forwardRef} from 'react';
@@ -44,6 +45,7 @@ function ProgressBar(props: ProgressBarProps, ref: ForwardedRef<HTMLDivElement>)
4445
maxValue = 100,
4546
isIndeterminate = false
4647
} = props;
48+
value = clamp(value, minValue, maxValue);
4749

4850
let [labelRef, label] = useSlot();
4951
let {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2022 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import {Label, Meter} from 'react-aria-components';
14+
import React from 'react';
15+
16+
export default {
17+
title: 'React Aria Components',
18+
component: Meter,
19+
args: {
20+
value: 50
21+
},
22+
argTypes: {
23+
value: {control: 'number'},
24+
minValue: {control: 'number'},
25+
maxValue: {control: 'number'}
26+
}
27+
};
28+
29+
export const MeterExample = (args) => {
30+
return (
31+
<Meter {...args}>
32+
{({percentage, valueText}) => (
33+
<>
34+
<Label>Storage space</Label>
35+
<span className="value">{valueText}</span>
36+
<div className="bar">
37+
<div className="fill" style={{width: percentage + '%'}} />
38+
</div>
39+
</>
40+
)}
41+
</Meter>
42+
);
43+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2022 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import {Label, ProgressBar} from 'react-aria-components';
14+
import React from 'react';
15+
16+
export default {
17+
title: 'React Aria Components',
18+
component: ProgressBar,
19+
args: {
20+
value: 50
21+
},
22+
argTypes: {
23+
value: {control: 'number'},
24+
minValue: {control: 'number'},
25+
maxValue: {control: 'number'}
26+
}
27+
};
28+
29+
export const ProgressBarExample = (args) => {
30+
return (
31+
<ProgressBar {...args}>
32+
{({percentage, valueText}) => (
33+
<>
34+
<Label>Storage space</Label>
35+
<span className="value">{valueText}</span>
36+
<div className="bar">
37+
<div className="fill" style={{width: percentage + '%'}} />
38+
</div>
39+
</>
40+
)}
41+
</ProgressBar>
42+
);
43+
};

packages/react-aria-components/stories/styles.css

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,33 @@
357357
opacity: 0.4;
358358
}
359359
}
360+
361+
:global(.react-aria-Meter),
362+
:global(.react-aria-ProgressBar) {
363+
--fill-color: forestgreen;
364+
365+
display: grid;
366+
grid-template-areas: "label value"
367+
"bar bar";
368+
grid-template-columns: 1fr auto;
369+
gap: 4px;
370+
width: 250px;
371+
color: var(--text-color);
372+
373+
:global(.value) {
374+
grid-area: value;
375+
}
376+
377+
:global(.bar) {
378+
grid-area: bar;
379+
box-shadow: inset 0px 0px 0px 1px var(--spectrum-global-color-gray-400);
380+
height: 10px;
381+
border-radius: 5px;
382+
overflow: hidden;
383+
}
384+
385+
:global(.fill) {
386+
background: var(--fill-color);
387+
height: 100%;
388+
}
389+
}

0 commit comments

Comments
 (0)