import { SharedStyle, StyleProp, tlmenus, useEditor } from '@tldraw/editor' import * as React from 'react' import { StyleValuesForUi } from '../../../styles' import { TLUiTranslationKey } from '../../hooks/useTranslation/TLUiTranslationKey' import { useTranslation } from '../../hooks/useTranslation/useTranslation' import { TLUiIconType } from '../../icon-types' import { TldrawUiButtonIcon } from '../primitives/Button/TldrawUiButtonIcon' import { TldrawUiButtonLabel } from '../primitives/Button/TldrawUiButtonLabel' import { TldrawUiMenuContextProvider } from '../primitives/menus/TldrawUiMenuContext' import { TldrawUiPopover, TldrawUiPopoverContent, TldrawUiPopoverTrigger, } from '../primitives/TldrawUiPopover' import { TldrawUiToolbar, TldrawUiToolbarButton } from '../primitives/TldrawUiToolbar' import { useStylePanelContext } from './StylePanelContext' /** @public */ export interface StylePanelDropdownPickerProps { id: string label?: TLUiTranslationKey | Exclude uiType: string stylePanelType: string style: StyleProp value: SharedStyle items: StyleValuesForUi type: 'icon' | 'tool' | 'menu' onValueChange?(style: StyleProp, value: T): void /** Override the test ID prefix. Defaults to uiType. */ testIdType?: string /** Distance to push the popover left of the trigger so it lands flush with the style panel. */ sideOffset?: number } function StylePanelDropdownPickerInner(props: StylePanelDropdownPickerProps) { const msg = useTranslation() const toolbarLabel = props.label ? msg(props.label) : msg(`style-panel.${props.stylePanelType}` as TLUiTranslationKey) return ( ) } function StylePanelDropdownPickerInlineInner( props: StylePanelDropdownPickerProps ) { const ctx = useStylePanelContext() const { id, label, uiType, stylePanelType, style, items, type, value, onValueChange = ctx.onValueChange, testIdType = uiType, sideOffset = 0, } = props const msg = useTranslation() const editor = useEditor() const [isOpen, setIsOpen] = React.useState(false) const icon = React.useMemo(() => { if (value.type === 'mixed') return 'mixed' as TLUiIconType const match = items.find((item) => item.value === value.value)?.icon return match ?? items[0]?.icon }, [items, value]) const stylePanelName = msg(`style-panel.${stylePanelType}` as TLUiTranslationKey) const titleStr = value.type === 'mixed' ? msg('style-panel.mixed') : stylePanelName + ' — ' + msg(`${uiType}-style.${value.value}` as TLUiTranslationKey) const labelStr = label ? msg(label) : '' const popoverId = `style panel ${id}` return ( {labelStr && {labelStr}} 4 ? 'grid' : 'horizontal'} label={labelStr}> {items.map((item) => { return ( { ctx.onHistoryMark('select style dropdown item') onValueChange(style, item.value) tlmenus.deleteOpenMenu(popoverId, editor.contextId) setIsOpen(false) }} > ) })} ) } // need to export like this to get generics /** @public @react */ export const StylePanelDropdownPicker = React.memo(StylePanelDropdownPickerInner) as < T extends string, >( props: StylePanelDropdownPickerProps ) => React.JSX.Element /** @public @react */ export const StylePanelDropdownPickerInline = React.memo(StylePanelDropdownPickerInlineInner) as < T extends string, >( props: StylePanelDropdownPickerProps ) => React.JSX.Element