Skip to content

Commit 60cdad6

Browse files
ktaborsGitHub Enterprise
authored andcommitted
Adding ContextualHelp to Slider, RangeSlider, TagGroup, and ColorSlider (#187)
* Adding ContextualHelp to Slider, RangeSlider, TagGroup, and ColorSlider * removing thumbLabels from stories --------- Co-authored-by: Kyle Taborski <[email protected]>
1 parent bd39f29 commit 60cdad6

File tree

6 files changed

+95
-16
lines changed

6 files changed

+95
-16
lines changed

packages/@react-spectrum/s2/src/ColorSlider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import {ColorHandle} from './ColorHandle';
2222
import {forwardRef, useRef} from 'react';
2323
import {StyleProps, getAllowedOverrides} from './style-utils' with {type: 'macro'};
2424
import {useDOMRef} from '@react-spectrum/utils';
25-
import {DOMRef} from '@react-types/shared';
25+
import {DOMRef, SpectrumLabelableProps} from '@react-types/shared';
2626

27-
export interface ColorSliderProps extends Omit<AriaColorSliderProps, 'children' | 'className' | 'style'>, StyleProps {
27+
export interface ColorSliderProps extends Omit<AriaColorSliderProps, 'children' | 'className' | 'style'>, Pick<SpectrumLabelableProps, 'contextualHelp'>, StyleProps {
2828
label?: string
2929
}
3030

@@ -65,7 +65,7 @@ function ColorSlider(props: ColorSliderProps, ref: DOMRef<HTMLDivElement>) {
6565
}, getAllowedOverrides())(renderProps, styles)}>
6666
{({isDisabled, orientation, state}) => (<>
6767
{orientation === 'horizontal' && props.label &&
68-
<FieldLabel isDisabled={isDisabled}>{props.label}</FieldLabel>
68+
<FieldLabel isDisabled={isDisabled} contextualHelp={props.contextualHelp}>{props.label}</FieldLabel>
6969
}
7070
{orientation === 'horizontal' &&
7171
<SliderOutput

packages/@react-spectrum/s2/src/Field.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function FieldLabel(props: FieldLabelProps, ref: DOMRef<HTMLLabelElement>) {
138138
<CenterBaseline
139139
className={style({
140140
display: 'inline-flex',
141-
contain: 'size',
141+
height: 0,
142142
marginStart: 4
143143
})}>
144144
<ContextualHelpContext.Provider

packages/@react-spectrum/s2/src/Slider.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
*/
1212

1313
import {
14-
Label,
1514
Slider as AriaSlider,
1615
SliderOutput,
1716
SliderProps as AriaSliderProps,
1817
SliderThumb,
1918
SliderTrack
2019
} from 'react-aria-components';
2120
import {FormContext, useFormProps} from './Form';
21+
import {FieldLabel} from './Field';
2222
import {ReactNode, useRef, forwardRef, RefObject, useContext} from 'react';
23-
import {FocusableRef, LabelPosition, InputDOMProps, Alignment} from '@react-types/shared';
23+
import {Alignment, FocusableRef, InputDOMProps, LabelPosition, SpectrumLabelableProps} from '@react-types/shared';
2424
import {useFocusableRef} from '@react-spectrum/utils';
2525
import {style, size} from '../style/spectrum-theme' with {type: 'macro'};
2626
import {StyleProps, focusRing, field, fieldInput, getAllowedOverrides} from './style-utils' with {type: 'macro'};
@@ -30,12 +30,10 @@ import {clamp} from '@react-aria/utils';
3030
import {mergeStyles} from '../style/runtime';
3131
import {pressScale} from './pressScale';
3232

33-
export interface SliderBaseProps<T> extends Omit<AriaSliderProps<T>, 'children'>, StyleProps, InputDOMProps {
33+
export interface SliderBaseProps<T> extends Omit<AriaSliderProps<T>, 'children'>, Omit<SpectrumLabelableProps, 'necessityIndicator' | 'isRequired'>, StyleProps, InputDOMProps {
3434
children?: ReactNode,
3535
label?: ReactNode,
36-
labelAlign?: Alignment,
37-
size?: 'S' | 'M' | 'L' | 'XL',
38-
labelPosition?: LabelPosition
36+
size?: 'S' | 'M' | 'L' | 'XL'
3937
}
4038

4139
export interface SliderProps<T> extends Omit<AriaSliderProps<T>, 'style' | 'className' | 'orientation'>, StyleProps {
@@ -118,7 +116,7 @@ const labelContainer = style({
118116
width: 'full',
119117
gridTemplateAreas: {
120118
labelPosition: {
121-
top: ['sliderLabel . output']
119+
top: ['label . output']
122120
}
123121
},
124122
gridTemplateColumns: {
@@ -137,6 +135,10 @@ const labelContainer = style({
137135
}
138136
}
139137
}
138+
},
139+
'--field-gap': {
140+
type: 'paddingBottom',
141+
value: 0
140142
}
141143
});
142144

@@ -347,7 +349,6 @@ export function SliderBase<T extends number | number[]>(props: SliderBaseProps<T
347349
let formatter = useNumberFormatter(formatOptions);
348350
let {direction} = useLocale();
349351

350-
351352
return (
352353
<AriaSlider
353354
{...props}
@@ -384,9 +385,14 @@ export function SliderBase<T extends number | number[]>(props: SliderBaseProps<T
384385
return (
385386
<>
386387
<div className={labelContainer({labelPosition, labelAlign})}>
387-
<Label className={style({gridArea: {labelPosition: {top: 'sliderLabel'}}})({labelPosition})}>
388+
<FieldLabel
389+
isDisabled={props.isDisabled}
390+
size={props.size}
391+
labelPosition={labelPosition}
392+
labelAlign={labelAlign}
393+
contextualHelp={props.contextualHelp}>
388394
{label}
389-
</Label>
395+
</FieldLabel>
390396
{labelPosition === 'top' && outputValue}
391397
</div>
392398
<div className={style({gridArea: 'input', display: 'inline-flex', alignItems: 'center', gap: {default: 16, size: {L: 20, XL: 24}}})({size})}>

packages/@react-spectrum/s2/src/TagGroup.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface TagProps extends Omit<AriaTagProps, 'children' | 'style' | 'cla
4343
children?: ReactNode
4444
}
4545

46-
export interface TagGroupProps<T> extends Omit<AriaTagGroupProps, 'children' | 'style' | 'className'>, Pick<TagListProps<T>, 'items' | 'children' | 'renderEmptyState'>, Omit<SpectrumLabelableProps, 'isRequired' | 'necessityIndicator' | 'contextualHelp'>, StyleProps, Omit<HelpTextProps, 'errorMessage'> {
46+
export interface TagGroupProps<T> extends Omit<AriaTagGroupProps, 'children' | 'style' | 'className'>, Pick<TagListProps<T>, 'items' | 'children' | 'renderEmptyState'>, Omit<SpectrumLabelableProps, 'isRequired' | 'necessityIndicator'>, StyleProps, Omit<HelpTextProps, 'errorMessage'> {
4747
/** A description for the tag group. */
4848
description?: ReactNode,
4949
/**
@@ -146,7 +146,8 @@ function TagGroup<T extends object>(
146146
<FieldLabel
147147
size={size}
148148
labelPosition={labelPosition}
149-
labelAlign={labelAlign}>
149+
labelAlign={labelAlign}
150+
contextualHelp={props.contextualHelp}>
150151
{label}
151152
</FieldLabel>
152153
<div

packages/@react-spectrum/s2/stories/RangeSlider.stories.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import {Content, Footer, Heading, Text} from '../src/Content';
14+
import {ContextualHelp} from '../src/ContextualHelp';
15+
import {Link} from '../src/Link';
1316
import {RangeSlider} from '../src';
1417
import type {Meta} from '@storybook/react';
1518

@@ -34,3 +37,34 @@ Example.args = {
3437
defaultValue: [30, 60],
3538
thumbLabels: ['start', 'end']
3639
};
40+
41+
export const ContextualHelpExample = (args: any) => <RangeSlider {...args} />;
42+
43+
ContextualHelpExample.args = {
44+
label: 'Range',
45+
defaultValue: [30, 60],
46+
contextualHelp: (
47+
<ContextualHelp>
48+
<Heading>What is a ice cream?</Heading>
49+
<Content>
50+
<Text>
51+
A combination of sugar, eggs, milk, and cream is cooked to make
52+
a custard base. Then, flavorings are added, and this flavored
53+
mixture is carefully churned and frozen to make ice cream.
54+
</Text>
55+
</Content>
56+
<Footer>
57+
<Link
58+
isStandalone
59+
href="https://en.wikipedia.org/wiki/Ice_cream"
60+
target="_blank">Learn more about ice cream</Link>
61+
</Footer>
62+
</ContextualHelp>
63+
)
64+
};
65+
66+
ContextualHelpExample.parameters = {
67+
docs: {
68+
disable: true
69+
}
70+
};

packages/@react-spectrum/s2/stories/Slider.stories.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import {Content, Footer, Heading, Text} from '../src/Content';
14+
import {ContextualHelp} from '../src/ContextualHelp';
15+
import {Link} from '../src/Link';
1316
import {Slider} from '../src/Slider';
1417
import type {Meta} from '@storybook/react';
1518
import {style} from '../style/spectrum-theme' with { type: 'macro' };
@@ -70,3 +73,38 @@ FormatOptions.args = {
7073
formatOptions: {style: 'currency', currency: 'JPY'},
7174
thumbLabel: 'currency'
7275
};
76+
77+
export const ContextualHelpExample = (args: any) => (
78+
<div style={{display: 'flex', flexDirection: 'column', alignItems: 'start', gap: 8}}>
79+
<Slider {...args} />
80+
</div>
81+
);
82+
83+
ContextualHelpExample.args = {
84+
label: 'Cookies',
85+
defaultValue: 30,
86+
contextualHelp: (
87+
<ContextualHelp>
88+
<Heading>What is a ice cream?</Heading>
89+
<Content>
90+
<Text>
91+
A combination of sugar, eggs, milk, and cream is cooked to make
92+
a custard base. Then, flavorings are added, and this flavored
93+
mixture is carefully churned and frozen to make ice cream.
94+
</Text>
95+
</Content>
96+
<Footer>
97+
<Link
98+
isStandalone
99+
href="https://en.wikipedia.org/wiki/Ice_cream"
100+
target="_blank">Learn more about ice cream</Link>
101+
</Footer>
102+
</ContextualHelp>
103+
)
104+
};
105+
106+
ContextualHelpExample.parameters = {
107+
docs: {
108+
disable: true
109+
}
110+
};

0 commit comments

Comments
 (0)