'use client'; import * as React from 'react'; import { classNames, isPrimitiveReactNode } from '@vkontakte/vkjs'; import { usePlatform } from '../../hooks/usePlatform'; import { hasAccessibleName } from '../../lib/accessibility'; import { COMMON_WARNINGS, warnOnce } from '../../lib/warnOnce'; import { Tappable, type TappableOmitProps } from '../Tappable/Tappable'; import { Text } from '../Typography/Text/Text'; import { Title } from '../Typography/Title/Title'; import styles from './PanelHeaderButton.module.css'; const platformClassNames = { ios: styles.ios, android: styles.android, vkcom: styles.vkcom, }; const defaultHoverMode = { ios: 'background', android: 'background', vkcom: styles.hover, }; const defaultActiveMode = { ios: 'opacity', android: 'background', vkcom: styles.active, }; export interface PanelHeaderButtonProps extends Omit { /** * Флаг для обозначения первичной кнопки * Влияет на стилизацию кнопки. */ primary?: boolean; /** * Текст или содержимое кнопки. */ label?: React.ReactNode; // TODO [>=8]: добавить св-во indicator, чтобы разграничить кейсы. } interface ButtonTypographyProps extends React.AllHTMLAttributes { /** * Флаг для обозначения первичной кнопки * Наследуется от PanelHeaderButtonProps['primary']. */ primary?: PanelHeaderButtonProps['primary']; } const ButtonTypography = ({ primary, children }: ButtonTypographyProps) => { const platform = usePlatform(); if (platform === 'ios') { return ( {children} ); } return {children}; }; const warn = warnOnce('PanelHeaderButton'); /** * @see https://vkui.io/components/panel-header#panel-header-button */ export const PanelHeaderButton = ({ children, primary = false, label, activeEffectDelay = 200, hoverMode, activeMode, ...restProps }: PanelHeaderButtonProps): React.ReactNode => { const isPrimitive = isPrimitiveReactNode(children); const isPrimitiveLabel = isPrimitiveReactNode(label); const platform = usePlatform(); if (process.env.NODE_ENV === 'development') { /* istanbul ignore next: проверка в dev mode, тест на hasAccessibleName() есть в lib/accessibility.test.tsx */ const isAccessible = hasAccessibleName({ children: [children, label], ...restProps, }); if (!isAccessible) { warn(COMMON_WARNINGS.a11y[restProps.href ? 'link-name' : 'button-name'], 'error'); } } const elements = [label, children].filter((item) => !!item); const onlyPrimitive = elements.length === 1 && isPrimitiveReactNode(elements[0]); return ( {isPrimitive ? {children} : children} {isPrimitiveLabel ? ( {label} ) : ( label )} ); };