Skip to content

Commit

Permalink
chore: checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSisb committed Aug 4, 2023
1 parent 345d24b commit 9389ccd
Show file tree
Hide file tree
Showing 4 changed files with 469 additions and 17 deletions.
9 changes: 7 additions & 2 deletions packages/paste-core/components/slider/src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import {useSliderState, useSlider, useSliderThumb} from '@twilio-paste/react-spe
import {SliderThumb} from './SliderThumb';
import {StyledTrack} from './StyledTrack';

const DefaultNumberFormatter = new Intl.NumberFormat('en-US');

export interface SliderProps {
element?: BoxProps['element'];
id: string;
'aria-describedby'?: string;
disabled?: boolean;
hasError?: boolean;
hideRangeLabels?: boolean;
Expand Down Expand Up @@ -52,6 +55,7 @@ export const Slider: React.FC<SliderProps> = (props) => {
isDisabled,
// needed to silence react-aria a11y guardrails
'aria-labelledby': id,
numberFormatter: props.numberFormatter || DefaultNumberFormatter,
};

// These hooks manage the state of the slider
Expand Down Expand Up @@ -92,10 +96,10 @@ export const Slider: React.FC<SliderProps> = (props) => {
color="colorTextWeak"
>
<Box as="span" element={`${element}_RANGE_LABELS_MIN`}>
{props.numberFormatter.format(minValue)}
{remappedProps.numberFormatter.format(minValue)}
</Box>
<Box as="span" element={`${element}_RANGE_LABELS_MAX`}>
{props.numberFormatter.format(maxValue)}
{remappedProps.numberFormatter.format(maxValue)}
</Box>
</Box>
)}
Expand Down Expand Up @@ -126,6 +130,7 @@ export const Slider: React.FC<SliderProps> = (props) => {
<input
ref={inputRef}
{...inputProps}
aria-describedby={props['aria-describedby']}
aria-labelledby={undefined}
id={inputProps.id?.replace('-0', '')}
onFocus={() => setFocused(true)}
Expand Down
36 changes: 21 additions & 15 deletions packages/paste-core/components/slider/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ const PercentFormatter = new Intl.NumberFormat('en-US', {style: 'percent'});

export const Default = (): React.ReactNode => {
const [value, setValue] = React.useState<number>(0.75);
const id = useUID();
const sliderId = useUID();
const helpTextId = useUID();

return (
<Form>
<FormControl>
<Label htmlFor={id}>Volume</Label>
<Label htmlFor={sliderId}>Volume</Label>
<Slider
id={id}
id={sliderId}
aria-describedby={helpTextId}
value={value}
minValue={0}
maxValue={1}
step={0.01}
onChange={(newValue) => setValue(newValue)}
numberFormatter={PercentFormatter}
/>
<HelpText>
I saw an ad yesterday that said “Radio for sale $1, volume is stuck on full blast. I said to myself “well, I
<HelpText id={helpTextId}>
I saw an ad yesterday that said “Radio for sale $1, volume is stuck on full blast. I said to myself “well, I
can’t turn that down.”
</HelpText>
</FormControl>
Expand All @@ -44,24 +46,26 @@ export const Default = (): React.ReactNode => {

export const Disabled = (): React.ReactNode => {
const [value, setValue] = React.useState<number>(0.01);
const id = useUID();
const sliderId = useUID();
const helpTextId = useUID();

return (
<Form>
<FormControl>
<Label htmlFor={id}>Volume</Label>
<Label htmlFor={sliderId}>Volume</Label>
<Slider
disabled
id={id}
id={sliderId}
aria-describedby={helpTextId}
value={value}
minValue={0}
maxValue={1}
step={0.01}
onChange={(newValue) => setValue(newValue)}
numberFormatter={PercentFormatter}
/>
<HelpText>
I saw an ad yesterday that said “Radio for sale $1, volume is stuck on full blast. I said to myself “well, I
<HelpText id={helpTextId}>
I saw an ad yesterday that said “Radio for sale $1, volume is stuck on full blast. I said to myself “well, I
can’t turn that down.”
</HelpText>
</FormControl>
Expand All @@ -71,26 +75,28 @@ export const Disabled = (): React.ReactNode => {

export const Error = (): React.ReactNode => {
const [value, setValue] = React.useState<number>(32);
const id = useUID();
const sliderId = useUID();
const helpTextId = useUID();

return (
<Form>
<FormControl>
<Label required htmlFor={id}>
<Label required htmlFor={sliderId}>
How many radios do you own?
</Label>
<Slider
hasError
id={id}
id={sliderId}
aria-describedby={helpTextId}
value={value}
minValue={0}
maxValue={100}
step={1}
onChange={(newValue) => setValue(newValue)}
numberFormatter={NumberFormatter}
/>
<HelpText variant="error">
I saw an ad yesterday that said “Radio for sale $1, volume is stuck on full blast. I said to myself “well, I
<HelpText id={helpTextId} variant="error">
I saw an ad yesterday that said “Radio for sale $1, volume is stuck on full blast. I said to myself “well, I
can’t turn that down.”
</HelpText>
</FormControl>
Expand Down
202 changes: 202 additions & 0 deletions packages/paste-website/src/component-examples/SliderExamples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
export const defaultSlider = `
const DefaultSliderExample = () => {
const [value, setValue] = React.useState(0.75);
const id = useUID();
const PercentFormatter = React.useMemo(() => {
return new Intl.NumberFormat('en-US', { style: 'percent' });
}, []);
return (
<Form>
<FormControl>
<Label htmlFor={id}>Volume</Label>
<Slider
id={id}
value={value}
minValue={0}
maxValue={1}
step={0.01}
onChange={(newValue) => setValue(newValue)}
numberFormatter={PercentFormatter}
/>
<HelpText>
I saw an ad yesterday that said “Radio for sale $1, volume is stuck on full blast”.
I said to myself “well, I can’t turn that down.”
</HelpText>
</FormControl>
</Form>
);
};
render(
<DefaultSliderExample />
)
`.trim();

export const disabledSlider = `
const DisabledSliderExample = () => {
const [value, setValue] = React.useState(0.75);
const id = useUID();
const PercentFormatter = React.useMemo(() => {
return new Intl.NumberFormat('en-US', { style: 'percent' });
}, []);
return (
<Form>
<FormControl>
<Label htmlFor={id}>Volume</Label>
<Slider
disabled
id={id}
value={value}
minValue={0}
maxValue={1}
step={0.01}
onChange={(newValue) => setValue(newValue)}
numberFormatter={PercentFormatter}
/>
<HelpText>
I saw an ad yesterday that said “Radio for sale $1, volume is stuck on full blast”.
I said to myself “well, I can’t turn that down.”
</HelpText>
</FormControl>
</Form>
);
};
render(
<DisabledSliderExample />
)
`.trim();

export const errorSlider = `
const ErrorSliderExample = () => {
const [value, setValue] = React.useState(20);
const id = useUID();
const NumberFormatter = React.useMemo(() => {
return new Intl.NumberFormat('en-US');
}, []);
const hasError = value > 10;
return (
<Form>
<FormControl>
<Label required htmlFor={id}>How many radios to purchase?</Label>
<Slider
hasError={hasError}
id={id}
value={value}
minValue={0}
maxValue={100}
step={1}
onChange={(newValue) => setValue(newValue)}
numberFormatter={NumberFormatter}
/>
<HelpText variant={hasError ? "error" : "default"}>
{hasError ? "Too many radios! You can only buy 10 radios." : "The number of radios you've selected for purchase."}
</HelpText>
</FormControl>
</Form>
);
};
render(
<ErrorSliderExample />
)
`.trim();

export const customRangeSlider = `
const CustomRangeSliderExample = () => {
const [value, setValue] = React.useState(0.55);
const id = useUID();
const PercentFormatter = React.useMemo(() => {
return new Intl.NumberFormat('en-US', { style: 'percent' });
}, []);
return (
<Form>
<FormControl>
<Label htmlFor={id}>Screen brightness</Label>
<Slider
id={id}
value={value}
minValue={0.5}
maxValue={0.8}
step={0.01}
onChange={(newValue) => setValue(newValue)}
numberFormatter={PercentFormatter}
/>
</FormControl>
</Form>
);
};
render(
<CustomRangeSliderExample />
)
`.trim();

export const customStepSlider = `
const CustomStepSliderExample = () => {
const [value, setValue] = React.useState(32);
const id = useUID();
const NumberFormatter = React.useMemo(() => {
return new Intl.NumberFormat('en-US');
}, []);
return (
<Form>
<FormControl>
<Label htmlFor={id}>Player count</Label>
<Slider
id={id}
value={value}
step={25}
onChange={(newValue) => setValue(newValue)}
numberFormatter={NumberFormatter}
/>
</FormControl>
</Form>
);
};
render(
<CustomStepSliderExample />
)
`.trim();

export const hiddenRangeLabelsSlider = `
const HiddenRangeLabelsSlider = () => {
const [value, setValue] = React.useState(32);
const id = useUID();
const NumberFormatter = React.useMemo(() => {
return new Intl.NumberFormat('en-US');
}, []);
return (
<Form>
<FormControl>
<Label htmlFor={id}>Scale</Label>
<Slider
id={id}
value={value}
onChange={(newValue) => setValue(newValue)}
numberFormatter={NumberFormatter}
hideRangeLabels
/>
</FormControl>
</Form>
);
};
render(
<HiddenRangeLabelsSlider />
)
`.trim();

0 comments on commit 9389ccd

Please sign in to comment.