-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Luna 1326: Changes BpkSlider base library for mobile accessibility #3368
Changes from 19 commits
743b3d0
6b7f74b
84a5687
83447bb
740740c
33c88bb
0c980b6
64ddcf2
5910325
5af4cd4
8dfdbb8
a43bd05
4b798bc
a67cf6d
52e0b41
2776391
70eed83
806078b
51800c4
6e9d1b1
7ee6105
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2016 Skyscanner Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import BpkSlider from './src/BpkSlider' | ||
import type { Props } from './src/BpkSlider' | ||
export type BpkSliderProps = Props | ||
export default BpkSlider |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ import { render } from '@testing-library/react'; | |
|
||
import BpkSlider from './BpkSlider'; | ||
|
||
// Mock the ResizeObserver which 'react-slider' uses to handle slider resize programatically | ||
// Mock the ResizeObserver which 'react-slider' uses to handle slider resize programatically | ||
window.ResizeObserver = | ||
window.ResizeObserver || | ||
jest.fn().mockImplementation(() => ({ | ||
|
@@ -30,35 +30,37 @@ window.ResizeObserver = | |
})); | ||
|
||
describe('BpkSlider', () => { | ||
const defaultProps = { | ||
min: 0, | ||
max: 100, | ||
value: 25, | ||
onChange: jest.fn(), | ||
onAfterChange: jest.fn(), | ||
ariaLabel: ['min', 'max'], | ||
ariaValuetext: ['0','80'] | ||
} | ||
it('should render correctly', () => { | ||
const { asFragment } = render(<BpkSlider min={0} max={100} value={25} />); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render correctly with a "className" attribute', () => { | ||
const { asFragment } = render( | ||
<BpkSlider min={0} max={100} value={25} className="my-slider" />, | ||
); | ||
Comment on lines
-38
to
-41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. className prop removed |
||
const { asFragment } = render(<BpkSlider {...defaultProps} />); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render correctly with a "step" attribute', () => { | ||
const { asFragment } = render( | ||
<BpkSlider min={0} max={100} value={2} step={10} />, | ||
<BpkSlider {...defaultProps} step={10} />, | ||
); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render correctly with a range of values', () => { | ||
const { asFragment } = render( | ||
<BpkSlider min={0} max={100} value={[10, 90]} />, | ||
<BpkSlider {...defaultProps} value={[10, 90]} />, | ||
); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render correctly with a minimum distance between controls', () => { | ||
const { asFragment } = render( | ||
<BpkSlider min={0} max={100} value={[10, 90]} minDistance={20} />, | ||
<BpkSlider {...defaultProps} value={[10, 90]} minDistance={20} />, | ||
); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2016 Skyscanner Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import type { Props as BpkSliderProps } from './BpkSlider' | ||
export type Props = BpkSliderProps | ||
declare const BpkSlider: ({ ariaLabel, ariaValuetext, max, min, minDistance, onAfterChange, onChange, step, value }: Props) => JSX.Element | ||
export default BpkSlider |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,58 +16,87 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import PropTypes from 'prop-types'; | ||
|
||
// @ts-expect-error Untyped import. See `decisions/imports-ts-suppressions.md`. | ||
import Slider from 'react-slider'; | ||
import * as Slider from '@radix-ui/react-slider'; | ||
|
||
import { cssModules, isRTL } from '../../bpk-react-utils'; | ||
|
||
import STYLES from './BpkSlider.module.scss'; | ||
|
||
const getClassName = cssModules(STYLES); | ||
|
||
type Props = { | ||
className?: string | null; | ||
export type Props = { | ||
max: number; | ||
min: number; | ||
minDistance?: number; | ||
step?: number; | ||
onChange: (value: number[] | number) => void; | ||
onAfterChange?: (value: number[] | number) => void; | ||
value: number[] | number; | ||
ariaLabel: string[]; | ||
ariaValuetext?: string[]; | ||
[rest: string]: any; | ||
} | ||
}; | ||
|
||
const BpkSlider = (props: Props) => { | ||
const { className, ...rest } = props; | ||
const BpkSlider = ({ | ||
ariaLabel, | ||
ariaValuetext, | ||
max, | ||
min, | ||
minDistance, | ||
onAfterChange, | ||
onChange, | ||
step, | ||
value, | ||
...rest | ||
}: Props) => { | ||
const invert = isRTL(); | ||
const classNames = [getClassName('bpk-slider')]; | ||
const thumbClassNames = [getClassName('bpk-slider__handle')]; | ||
const trackClassNames = [getClassName('bpk-slider__bar')]; | ||
const currentValue = Array.isArray(value) ? value : [value]; | ||
|
||
const isRange = (rest.value || rest.defaultValue || []).length > 1; | ||
const processSliderValues = ( | ||
sliderValues: number[], | ||
callback?: (val: number | number[]) => void, | ||
) => { | ||
const val = sliderValues.length === 1 ? sliderValues[0] : sliderValues; | ||
if (callback) { | ||
callback(val); | ||
} | ||
}; | ||
|
||
if (isRange) { | ||
classNames.push(getClassName('bpk-slider--range')); | ||
} | ||
if (className) { | ||
classNames.push(getClassName(className)); | ||
} | ||
const handleOnChange = (sliderValues: number[]) => { | ||
processSliderValues(sliderValues, onChange); | ||
}; | ||
|
||
const handleOnAfterChange = (sliderValues: number[]) => { | ||
processSliderValues(sliderValues, onAfterChange); | ||
}; | ||
|
||
return ( | ||
<Slider | ||
<Slider.Root | ||
className={getClassName('bpk-slider')} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it that we remove className throughout, when we still need it here for the root styling? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still need to give the slider skyscanner the styles but removing the className prop from BpkSlider prevents the likes of Banana adding their own styles on top of that |
||
defaultValue={currentValue} | ||
min={min} | ||
max={max} | ||
step={step || 1} | ||
onValueChange={handleOnChange} | ||
onValueCommit={handleOnAfterChange} | ||
inverted={invert} | ||
minStepsBetweenThumbs={minDistance} | ||
{...rest} | ||
withTracks | ||
snapDragDisabled={false} | ||
invert={invert} | ||
className={classNames.join(' ')} | ||
thumbClassName={thumbClassNames.join(' ')} | ||
thumbActiveClassName={getClassName('bpk-slider__handle--active')} | ||
trackClassName={trackClassNames.join(' ')} | ||
/> | ||
> | ||
<Slider.Track className={getClassName('bpk-slider__track')}> | ||
<Slider.Range className={getClassName('bpk-slider__range')} /> | ||
</Slider.Track> | ||
{currentValue.map((val, index) => ( | ||
<Slider.Thumb | ||
key={ariaLabel[index]} | ||
aria-label={ariaLabel[index]} | ||
aria-valuetext={ariaValuetext ? ariaValuetext[index] : val.toString()} | ||
className={getClassName('bpk-slider__thumb')} | ||
aria-valuenow={currentValue[index]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might fix the android issue. Without it the valueText remained static when the slider moved but with it it changes with the slider value Before Screen.Recording.2024-04-18.at.11.30.37.movNow Screen.Recording.2024-04-18.at.11.31.01.mov |
||
/> | ||
))} | ||
</Slider.Root> | ||
); | ||
}; | ||
|
||
BpkSlider.propTypes = { | ||
className: PropTypes.string, | ||
}; | ||
|
||
BpkSlider.defaultProps = { | ||
className: null, | ||
}; | ||
|
||
export default BpkSlider; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks nice when rendered