Skip to content

Commit

Permalink
feat(Switch): fix rtl view (#8104)
Browse files Browse the repository at this point in the history
* feat(Switch): fix rtl view

* test(Switch): add tests

* fix(Switch): fix prettier
  • Loading branch information
EldarMuhamethanov authored Dec 20, 2024
1 parent 4e0f4e1 commit c059b04
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ export const SwitchPlayground = (props: ComponentPlaygroundProps) => {
{
checked: [true, false],
disabled: [true, false],
dir: ['ltr', 'rtl'],
},
{
$adaptivity: 'y',
},
]}
>
{(props: SwitchProps) => <Switch {...props} />}
{({ dir, ...props }: SwitchProps) => (
<div dir={dir}>
<Switch {...props} />
</div>
)}
</ComponentPlayground>
);
};
Expand Down
14 changes: 11 additions & 3 deletions packages/vkui/src/components/Switch/Switch.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
/* Switch__track */
--vkui_internal--Switch__track_shape: calc(var(--vkui_internal--Switch__track_width) / 2);
--vkui_internal--Switch__track_outline_width: 2px;
--vkui_internal--Switch__handle_shift_x: var(--vkui_internal--Switch__track_outline_width);
--vkui_internal--Switch__handle_shift_x_direction: 1;
--vkui_internal--Switch__handle_shift_x: calc(
var(--vkui_internal--Switch__handle_shift_x_direction) *
var(--vkui_internal--Switch__track_outline_width)
);
/* prettier-ignore */
--vkui_internal--Switch__handle_shift_y: calc(var(--vkui_internal--Switch__track_height) - var(--vkui_internal--Switch__handle_height) - var(--vkui_internal--Switch__track_outline_width));
/* prettier-ignore */
Expand All @@ -16,6 +20,10 @@
-webkit-tap-highlight-color: transparent;
}

.rtl {
--vkui_internal--Switch__handle_shift_x_direction: -1;
}

.disabled {
cursor: not-allowed;
opacity: var(--vkui_internal--Switch_disabled);
Expand Down Expand Up @@ -136,7 +144,7 @@

/* Switch__handle */
/* prettier-ignore */
--vkui_internal--Switch__handle_shift_x: calc(var(--vkui_internal--Switch__track_width) - var(--vkui_internal--Switch__handle_width) - var(--vkui_internal--Switch__track_outline_width));
--vkui_internal--Switch__handle_shift_x: calc(var(--vkui_internal--Switch__handle_shift_x_direction) * calc(var(--vkui_internal--Switch__track_width) - var(--vkui_internal--Switch__handle_width) - var(--vkui_internal--Switch__track_outline_width)));
/* prettier-ignore */
--vkui_internal--Switch__handle_scale: calc(var(--vkui_internal--Switch__handle_widthNoUnit--checked) / var(--vkui_internal--Switch__handle_heightNoUnit));
--vkui_internal--Switch__track_shadow_visibility: 0;
Expand Down Expand Up @@ -212,7 +220,7 @@
border-radius: inherit;
background-color: var(--vkui_internal--Switch__handleRipple_color);
opacity: var(--vkui_internal--Switch__handleRipple_opacity);
transform: translate(-50%, -50%);
transform: translate(calc(-50% * var(--vkui_internal--Switch__handle_shift_x_direction)), -50%);
transition-property: opacity, background-color;
/* prettier-ignore */
transition-duration:
Expand Down
8 changes: 7 additions & 1 deletion packages/vkui/src/components/Switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import { useAdaptivity } from '../../hooks/useAdaptivity';
import { useDirection } from '../../hooks/useDirection';
import { useExternRef } from '../../hooks/useExternRef';
import { useFocusVisible } from '../../hooks/useFocusVisible';
import { useFocusVisibleClassName } from '../../hooks/useFocusVisibleClassName';
import { usePlatform } from '../../hooks/usePlatform';
Expand Down Expand Up @@ -36,6 +38,9 @@ export const Switch = ({
onClick,
...restProps
}: SwitchProps): React.ReactNode => {
const [directionRef, textDirection = 'ltr'] = useDirection<HTMLLabelElement>();
const isRtl = textDirection === 'rtl';
const rootRef = useExternRef<HTMLLabelElement>(getRootRef, directionRef);
const platform = usePlatform();
const { sizeY = 'none' } = useAdaptivity();
const { focusVisible, onBlur, onFocus } = useFocusVisible();
Expand Down Expand Up @@ -86,11 +91,12 @@ export const Switch = ({
sizeY !== 'regular' && sizeYClassNames[sizeY],
platform === 'ios' ? styles.ios : styles.default,
disabled && styles.disabled,
isRtl && styles.rtl,
focusVisibleClassNames,
className,
)}
style={style}
ref={getRootRef}
ref={rootRef}
>
<VisuallyHidden
{...inputProps}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c059b04

Please sign in to comment.