Skip to content
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

feat(Calendar): fix view in rtl #8068

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export const CalendarPlayground = (props: ComponentPlaygroundProps) => {
minDateTime: [new Date('1970-05-03')],
maxDateTime: [new Date('1970-05-10')],
},
{
value: [new Date('1970-05-05')],
dir: ['rtl'],
},
]}
>
{(props: CalendarProps) => <Calendar {...props} />}
Expand Down
150 changes: 86 additions & 64 deletions packages/vkui/src/components/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import { isSameDay, isSameMonth } from 'date-fns';
import {
CalendarDirectionContext,
type CalendarDirectionContextProps,
} from '../../context/CalendarDirectionContext';
import { useCalendar } from '../../hooks/useCalendar';
import { useDirection } from '../../hooks/useDirection';
import { useExternRef } from '../../hooks/useExternRef';
import { clamp, isFirstDay, isLastDay, navigateDate, setTimeEqual } from '../../lib/calendar';
import { useIsomorphicLayoutEffect } from '../../lib/useIsomorphicLayoutEffect';
import { warnOnce } from '../../lib/warnOnce';
Expand Down Expand Up @@ -99,6 +105,7 @@ const warn = warnOnce('Calendar');
* @see https://vkcom.github.io/VKUI/#/Calendar
*/
export const Calendar = ({
getRootRef,
value,
onChange,
disablePast,
Expand Down Expand Up @@ -167,6 +174,8 @@ export const Calendar = ({
minDateTime,
maxDateTime,
});
const [directionRef, textDirection = 'ltr'] = useDirection();
const rootRef = useExternRef(directionRef, getRootRef);

useIsomorphicLayoutEffect(() => {
if (value) {
Expand Down Expand Up @@ -214,70 +223,83 @@ export const Calendar = ({
[value],
);

const directionContextValue = React.useMemo<CalendarDirectionContextProps>(
() => ({
direction: textDirection,
}),
[textDirection],
);

return (
<RootComponent {...props} baseClassName={classNames(styles.host, size === 's' && styles.sizeS)}>
<CalendarHeader
viewDate={externalViewDate || viewDate}
onChange={setViewDate}
onNextMonth={setNextMonth}
onPrevMonth={setPrevMonth}
disablePickers={disablePickers || size === 's'}
className={styles.header}
prevMonthLabel={prevMonthLabel}
nextMonthLabel={nextMonthLabel}
changeMonthLabel={changeMonthLabel}
changeYearLabel={changeYearLabel}
prevMonthIcon={prevMonthIcon}
nextMonthIcon={nextMonthIcon}
prevMonthProps={prevMonthProps}
nextMonthProps={nextMonthProps}
isMonthDisabled={isMonthDisabled}
isYearDisabled={isYearDisabled}
nextMonthButtonTestId={nextMonthButtonTestId}
prevMonthButtonTestId={prevMonthButtonTestId}
monthDropdownTestId={monthDropdownTestId}
yearDropdownTestId={yearDropdownTestId}
/>
<CalendarDays
viewDate={externalViewDate || viewDate}
value={value}
weekStartsOn={weekStartsOn}
isDayFocused={isDayFocused}
tabIndex={0}
aria-label={changeDayLabel}
onKeyDown={handleKeyDown}
onDayChange={onDayChange}
isDayActive={isDayActive}
isDaySelectionStart={isFirstDay}
isDaySelectionEnd={isLastDay}
isDayDisabled={isDayDisabled}
onBlur={resetSelectedDay}
showNeighboringMonth={showNeighboringMonth}
size={size}
dayProps={dayProps}
listenDayChangesForUpdate={listenDayChangesForUpdate}
renderDayContent={renderDayContent}
dayTestId={dayTestId}
/>
{enableTime && value && size !== 's' && (
<div className={styles.time}>
<CalendarTime
value={value}
onChange={onChange}
onDoneButtonClick={onDoneButtonClick}
doneButtonText={doneButtonText}
doneButtonDisabled={doneButtonDisabled}
doneButtonShow={doneButtonShow}
DoneButton={DoneButton}
changeHoursLabel={changeHoursLabel}
changeMinutesLabel={changeMinutesLabel}
isDayDisabled={minDateTime || maxDateTime ? isDayDisabled : undefined}
minutesTestId={minutesTestId}
hoursTestId={hoursTestId}
doneButtonTestId={doneButtonTestId}
/>
</div>
)}
</RootComponent>
<CalendarDirectionContext.Provider value={directionContextValue}>
<RootComponent
{...props}
baseClassName={classNames(styles.host, size === 's' && styles.sizeS)}
getRootRef={rootRef}
>
<CalendarHeader
viewDate={externalViewDate || viewDate}
onChange={setViewDate}
onNextMonth={setNextMonth}
onPrevMonth={setPrevMonth}
disablePickers={disablePickers || size === 's'}
className={styles.header}
prevMonthLabel={prevMonthLabel}
nextMonthLabel={nextMonthLabel}
changeMonthLabel={changeMonthLabel}
changeYearLabel={changeYearLabel}
prevMonthIcon={prevMonthIcon}
nextMonthIcon={nextMonthIcon}
prevMonthProps={prevMonthProps}
nextMonthProps={nextMonthProps}
isMonthDisabled={isMonthDisabled}
isYearDisabled={isYearDisabled}
nextMonthButtonTestId={nextMonthButtonTestId}
prevMonthButtonTestId={prevMonthButtonTestId}
monthDropdownTestId={monthDropdownTestId}
yearDropdownTestId={yearDropdownTestId}
/>
<CalendarDays
viewDate={externalViewDate || viewDate}
value={value}
weekStartsOn={weekStartsOn}
isDayFocused={isDayFocused}
tabIndex={0}
aria-label={changeDayLabel}
onKeyDown={handleKeyDown}
onDayChange={onDayChange}
isDayActive={isDayActive}
isDaySelectionStart={isFirstDay}
isDaySelectionEnd={isLastDay}
isDayDisabled={isDayDisabled}
onBlur={resetSelectedDay}
showNeighboringMonth={showNeighboringMonth}
size={size}
dayProps={dayProps}
listenDayChangesForUpdate={listenDayChangesForUpdate}
renderDayContent={renderDayContent}
dayTestId={dayTestId}
/>
{enableTime && value && size !== 's' && (
<div className={styles.time}>
<CalendarTime
value={value}
onChange={onChange}
onDoneButtonClick={onDoneButtonClick}
doneButtonText={doneButtonText}
doneButtonDisabled={doneButtonDisabled}
doneButtonShow={doneButtonShow}
DoneButton={DoneButton}
changeHoursLabel={changeHoursLabel}
changeMinutesLabel={changeMinutesLabel}
isDayDisabled={minDateTime || maxDateTime ? isDayDisabled : undefined}
minutesTestId={minutesTestId}
hoursTestId={hoursTestId}
doneButtonTestId={doneButtonTestId}
/>
</div>
)}
</RootComponent>
</CalendarDirectionContext.Provider>
);
};
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.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
transform: translateX(-50%);
}

.rtl .today .dayNumber::after {
transform: translateX(50%);
}
inomdzhon marked this conversation as resolved.
Show resolved Hide resolved

.today .innerActive .dayNumber::after {
border-block-end-color: var(--vkui--color_stroke_contrast);
}
Expand Down
8 changes: 7 additions & 1 deletion packages/vkui/src/components/CalendarDay/CalendarDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import { useCalendarDirectionContext } from '../../context/CalendarDirectionContext';
import { ENABLE_KEYBOARD_INPUT_EVENT_NAME } from '../../hooks/useKeyboardInputTracker';
import { useConfigProvider } from '../ConfigProvider/ConfigProviderContext';
import { Tappable } from '../Tappable/Tappable';
Expand Down Expand Up @@ -68,6 +69,7 @@ export const CalendarDay: React.FC<CalendarDayProps> = React.memo(
}: CalendarDayProps) => {
const { locale } = useConfigProvider();
const ref = React.useRef<HTMLElement>(null);
const { direction } = useCalendarDirectionContext();
const onClick = React.useCallback(() => onChange(day), [day, onChange]);
const handleEnter = React.useCallback(() => onEnter?.(day), [day, onEnter]);
const handleLeave = React.useCallback(() => onLeave?.(day), [day, onLeave]);
Expand Down Expand Up @@ -104,7 +106,11 @@ export const CalendarDay: React.FC<CalendarDayProps> = React.memo(

return (
<Tappable
baseClassName={classNames(styles.host, size === 's' && styles.sizeS)}
baseClassName={classNames(
styles.host,
size === 's' && styles.sizeS,
direction === 'rtl' && styles.rtl,
)}
hoverMode={styles.hostHovered}
activeMode={styles.hostActivated}
hasActive={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@vkontakte/icons';
import { classNames } from '@vkontakte/vkjs';
import { addMonths, setMonth, setYear, subMonths } from 'date-fns';
import { useCalendarDirectionContext } from '../../context/CalendarDirectionContext';
import { DEFAULT_MAX_YEAR, DEFAULT_MIN_YEAR, getMonths, getYears } from '../../lib/calendar';
import type { HTMLAttributesWithRootRef } from '../../types';
import { AdaptivityProvider } from '../AdaptivityProvider/AdaptivityProvider';
Expand Down Expand Up @@ -104,6 +105,8 @@ export const CalendarHeader = ({
...restProps
}: CalendarHeaderProps): React.ReactNode => {
const { locale } = useConfigProvider();
const { direction } = useCalendarDirectionContext();

const onMonthsChange = React.useCallback(
(_: ChangeEvent<HTMLSelectElement>, newValue: SelectProps['value']) =>
onChange(setMonth(viewDate, Number(newValue))),
Expand Down Expand Up @@ -176,7 +179,7 @@ export const CalendarHeader = ({
<VisuallyHidden>
{prevMonthLabel}, {formatter.format(subMonths(viewDate, 1))}
</VisuallyHidden>
{prevMonthIcon}
{direction === 'ltr' ? prevMonthIcon : nextMonthIcon}
</Tappable>
</AdaptivityProvider>
)}
Expand Down Expand Up @@ -242,7 +245,7 @@ export const CalendarHeader = ({
<VisuallyHidden>
{nextMonthLabel}, {formatter.format(addMonths(viewDate, 1))}
</VisuallyHidden>
{nextMonthIcon}
{direction === 'ltr' ? nextMonthIcon : prevMonthIcon}
</Tappable>
</AdaptivityProvider>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export const CalendarRangePlayground = (props: ComponentPlaygroundProps) => {
</span>,
],
},
{
value: [[new Date('1970-05-05'), new Date('1970-06-05')]],
dir: ['rtl'],
},
]}
>
{(props: CalendarRangeProps) => <CalendarRange {...props} />}
Expand Down
Loading
Loading