Skip to content

Commit

Permalink
Merge pull request #1291 from core-ds/feat/use-match-media-default
Browse files Browse the repository at this point in the history
feat: add defaultMatchMediaValue
  • Loading branch information
fulcanellee authored Jul 16, 2024
2 parents c4e113c + 1979b0e commit b2ebcd9
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 27 deletions.
19 changes: 19 additions & 0 deletions .changeset/smart-nails-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@alfalab/core-components-calendar': minor
'@alfalab/core-components-checkbox-group': minor
'@alfalab/core-components-code-input': minor
'@alfalab/core-components-confirmation': minor
'@alfalab/core-components-custom-picker-button': minor
'@alfalab/core-components-filter-tag': minor
'@alfalab/core-components-mq': minor
'@alfalab/core-components-pass-code': minor
'@alfalab/core-components-pattern-lock': minor
'@alfalab/core-components-plate': minor
'@alfalab/core-components-radio-group': minor
'@alfalab/core-components-tag': minor
'@alfalab/core-components-toast': minor
'@alfalab/core-components-toast-plate': minor
'@alfalab/core-components-typography': minor
---

- Добавлен пропс defaultMatchMediaValue. С помощью него можно задавать fallback значение для хука useMatchMedia внутри компонента.
9 changes: 7 additions & 2 deletions packages/calendar/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ export type ResponsiveCalendarProps = CalendarDesktopProps &
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const CalendarResponsive = forwardRef<HTMLDivElement, ResponsiveCalendarProps>(
({ breakpoint = 1024, ...restProps }, ref) => {
const [isDesktop] = useMatchMedia(`(min-width: ${breakpoint}px)`);
({ breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const [isDesktop] = useMatchMedia(`(min-width: ${breakpoint}px)`, defaultMatchMediaValue);

return isDesktop ? (
<CalendarDesktop {...restProps} ref={ref} />
Expand Down
13 changes: 11 additions & 2 deletions packages/checkbox-group/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ export type CheckboxGroupProps = Omit<BaseCheckboxGroupProps, 'styles'> & {
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const CheckboxGroup: FC<CheckboxGroupProps> = ({ breakpoint = 1024, ...restProps }) => {
export const CheckboxGroup: FC<CheckboxGroupProps> = ({
breakpoint = 1024,
defaultMatchMediaValue,
...restProps
}) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? CheckboxGroupDesktop : CheckboxGroupMobile;

Expand Down
9 changes: 7 additions & 2 deletions packages/code-input/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ export type CodeInputProps = Omit<BaseCodeInputProps, 'stylesInput'> & {
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const CodeInput = forwardRef<CustomInputRef, CodeInputProps>(
({ breakpoint = 1024, ...restProps }, ref) => {
({ breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? CodeInputDesktop : CodeInputMobile;

Expand Down
8 changes: 7 additions & 1 deletion packages/confirmation/src/component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ export type ResponsiveConfirmationProps = Omit<
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const ConfirmationResponsive: FC<ResponsiveConfirmationProps> = ({
breakpoint = 1024,
defaultMatchMediaValue = true,
...restProps
}) => {
const [isDesktop] = useMatchMedia(`(min-width: ${breakpoint}px)`, true);
const [isDesktop] = useMatchMedia(`(min-width: ${breakpoint}px)`, defaultMatchMediaValue);

return isDesktop ? (
<ConfirmationDesktop {...restProps} />
Expand Down
8 changes: 7 additions & 1 deletion packages/custom-picker-button/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export type CustomPickerButtonResponsiveProps = CustomPickerButtonDesktopProps &
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const CustomPickerButtonResponsive = forwardRef<
Expand All @@ -33,12 +38,13 @@ export const CustomPickerButtonResponsive = forwardRef<
swipeable,
bottomSheetProps,
breakpoint = 1024,
defaultMatchMediaValue,
...restProps
},
ref,
) => {
const query = `(min-width: ${breakpoint}px)`;
const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

return isDesktop ? (
<CustomPickerButtonDesktop
Expand Down
9 changes: 7 additions & 2 deletions packages/filter-tag/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ export type FilterTagProps = Omit<BaseFilterTagProps, 'styles'> & {
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const FilterTag = forwardRef<HTMLDivElement, FilterTagProps>(
({ children, breakpoint = 1024, ...restProps }, ref) => {
({ children, breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? FilterTagDesktop : FilterTagMobile;

Expand Down
15 changes: 13 additions & 2 deletions packages/mq/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export type MqProps = {
*/
touch?: boolean;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);

/**
* Дочерние элементы.
*/
Expand All @@ -28,8 +33,14 @@ export type MqProps = {
onMatchChange?: (isMatched: boolean) => void;
};

export const Mq: FC<MqProps> = ({ children, query = '', touch, onMatchChange }) => {
const [queryMatches] = useMatchMedia(query);
export const Mq: FC<MqProps> = ({
children,
query = '',
touch,
defaultMatchMediaValue,
onMatchChange,
}) => {
const [queryMatches] = useMatchMedia(query, defaultMatchMediaValue);

const touchPass = touch === undefined ? true : touch === SUPPORTS_TOUCH;

Expand Down
4 changes: 2 additions & 2 deletions packages/pass-code/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { PassCodeMobile } from './mobile/PassCodeMobile';
import { PassCodeProps } from './typings';

export const PassCode = forwardRef<HTMLDivElement, PassCodeProps>(
({ breakpoint = 1024, ...restProps }, ref) => {
({ breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? PassCodeDesktop : PassCodeMobile;

Expand Down
5 changes: 5 additions & 0 deletions packages/pass-code/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export type BasePassCodeProps = {
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);

/**
* Стили компонента
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/pattern-lock/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { PatternLockMobile } from './mobile';
import { PatternLockProps } from './typings';

export const PatternLock = forwardRef<TPatternLockInstance, PatternLockProps>(
({ breakpoint = 1024, ...restProps }, ref) => {
({ breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? PatternLockDesktop : PatternLockMobile;

Expand Down
5 changes: 5 additions & 0 deletions packages/pattern-lock/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export type PatternLockProps = CommonPatternLockProps & {
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};
export type ObservableTokens = {
ACCENT_INITIAL: string;
Expand Down
9 changes: 7 additions & 2 deletions packages/plate/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ export type PlateProps = Omit<BasePlateProps, 'styles'> & {
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const Plate = forwardRef<HTMLDivElement, PlateProps>(
({ children, breakpoint = 1024, ...restProps }, ref) => {
({ children, breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? PlateDesktop : PlateMobile;

Expand Down
9 changes: 7 additions & 2 deletions packages/radio-group/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ export type RadioGroupProps = Omit<BaseRadioGroupProps, 'styles'> & {
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const RadioGroup = forwardRef<HTMLDivElement, RadioGroupProps>(
({ breakpoint = 1024, ...restProps }, ref) => {
({ breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? RadioGroupDesktop : RadioGroupMobile;

Expand Down
9 changes: 7 additions & 2 deletions packages/tag/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ export type TagProps = Omit<BaseTagProps, 'styles' | 'colorStylesMap'> & {
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const Tag = forwardRef<HTMLButtonElement, TagProps>(
({ children, breakpoint = 1024, ...restProps }, ref) => {
({ children, breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? TagDesktop : TagMobile;

Expand Down
9 changes: 7 additions & 2 deletions packages/toast-plate/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ export type ToastPlateProps = Omit<BaseToastPlateProps, 'styles'> & {
* @default 1024
*/
breakpoint?: number;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const ToastPlate = forwardRef<HTMLDivElement, ToastPlateProps>(
({ children, breakpoint = 1024, ...restProps }, ref) => {
({ children, breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? ToastPlateDesktop : ToastPlateMobile;

Expand Down
4 changes: 2 additions & 2 deletions packages/toast/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { ToastMobile } from './mobile';
export type ToastProps = BaseToastProps;

export const Toast = forwardRef<HTMLDivElement, ToastProps>(
({ children, breakpoint = 1024, ...restProps }, ref) => {
({ children, breakpoint = 1024, defaultMatchMediaValue, ...restProps }, ref) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query);
const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);

const Component = isDesktop ? ToastDesktop : ToastMobile;

Expand Down
3 changes: 2 additions & 1 deletion packages/typography/src/title-responsive/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import styles from './index.module.css';

export const TitleResponsive = forwardRef<HTMLHeadingElement | HTMLDivElement, TitleProps>(
(props, ref) => {
const [isDesktop] = useMatchMedia('(min-width: 1024px)');
const { defaultMatchMediaValue } = props;
const [isDesktop] = useMatchMedia('(min-width: 1024px)', defaultMatchMediaValue);

return (
<TitleBase
Expand Down
5 changes: 5 additions & 0 deletions packages/typography/src/title/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export type TitleProps = Omit<NativeProps, 'color'> & {
* Пропы для скелетона
*/
skeletonProps?: TextSkeletonProps;

/**
* Значение по-умолчанию для хука useMatchMedia
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

type PrivateProps = {
Expand Down

0 comments on commit b2ebcd9

Please sign in to comment.