|
| 1 | +import {EditorConfigStore} from '@codeimage/store/editor/config.store'; |
| 2 | +import {Box, FlexField, HStack, Text, VStack} from '@codeimage/ui'; |
| 3 | +import { |
| 4 | + As, |
| 5 | + IconButton, |
| 6 | + icons, |
| 7 | + Listbox, |
| 8 | + Popover, |
| 9 | + PopoverContent, |
| 10 | + PopoverTrigger, |
| 11 | +} from '@codeui/kit'; |
| 12 | +import {useModality} from '@core/hooks/isMobile'; |
| 13 | +import {DynamicSizedContainer} from '@ui/DynamicSizedContainer/DynamicSizedContainer'; |
| 14 | +import { |
| 15 | + ExperimentalFeatureTooltip, |
| 16 | + ExperimentalIcon, |
| 17 | +} from '@ui/ExperimentalFeatureTooltip/ExperimentalFeatureTooltip'; |
| 18 | +import {SegmentedField} from '@ui/SegmentedField/SegmentedField'; |
| 19 | +import {createSignal, Match, Switch} from 'solid-js'; |
| 20 | +import {provideState} from 'statebuilder'; |
| 21 | +import {CloseIcon} from '../../../Icons/CloseIcon'; |
| 22 | +import * as styles from './FontPicker.css'; |
| 23 | +import {createFontPickerListboxProps} from './FontPickerListbox'; |
| 24 | +import {FontSystemPicker} from './FontSystemPicker'; |
| 25 | + |
| 26 | +interface FontPickerProps { |
| 27 | + value: string; |
| 28 | + onChange: (value: string) => void; |
| 29 | +} |
| 30 | + |
| 31 | +type FontPickerModality = 'default' | 'system'; |
| 32 | + |
| 33 | +/** |
| 34 | + * @experimental |
| 35 | + */ |
| 36 | +export function FontPicker(props: FontPickerProps) { |
| 37 | + const [open, setOpen] = createSignal(false); |
| 38 | + const [mode, setMode] = createSignal<FontPickerModality>('default'); |
| 39 | + const modality = useModality(); |
| 40 | + const configState = provideState(EditorConfigStore); |
| 41 | + |
| 42 | + const webListboxItems = () => |
| 43 | + configState.get.fonts |
| 44 | + .filter(font => font.type === 'web') |
| 45 | + .map(font => ({ |
| 46 | + label: font.name, |
| 47 | + value: font.id, |
| 48 | + })); |
| 49 | + |
| 50 | + const webListboxProps = createFontPickerListboxProps({ |
| 51 | + onEsc: () => setOpen(false), |
| 52 | + onChange: props.onChange, |
| 53 | + get value() { |
| 54 | + return props.value; |
| 55 | + }, |
| 56 | + get items() { |
| 57 | + return webListboxItems(); |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + const selectedFont = () => |
| 62 | + [...configState.get.fonts, ...configState.get.systemFonts].find( |
| 63 | + font => font.id === props.value, |
| 64 | + ); |
| 65 | + |
| 66 | + return ( |
| 67 | + <Popover |
| 68 | + placement={modality === 'mobile' ? undefined : 'right-end'} |
| 69 | + open={open()} |
| 70 | + onOpenChange={setOpen} |
| 71 | + > |
| 72 | + <PopoverTrigger asChild> |
| 73 | + <As component={'div'} class={styles.input}> |
| 74 | + <span class={styles.inputValue}> |
| 75 | + {selectedFont()?.name ?? 'No font selected'} |
| 76 | + </span> |
| 77 | + <icons.SelectorIcon class={styles.inputIcon} /> |
| 78 | + </As> |
| 79 | + </PopoverTrigger> |
| 80 | + <PopoverContent variant={'bordered'} class={styles.fontPickerPopover}> |
| 81 | + <Box |
| 82 | + display={'flex'} |
| 83 | + justifyContent={'spaceBetween'} |
| 84 | + alignItems={'center'} |
| 85 | + marginBottom={4} |
| 86 | + > |
| 87 | + <ExperimentalFeatureTooltip feature={'Aspect ratio'}> |
| 88 | + <HStack spacing={'2'} alignItems={'flexEnd'}> |
| 89 | + <Text weight={'semibold'}>Fonts</Text> |
| 90 | + <Text class={styles.experimentalFlag} size={'xs'}> |
| 91 | + <Box as={'span'} display={'flex'} alignItems={'center'}> |
| 92 | + <ExperimentalIcon size={'xs'} /> |
| 93 | + <Box marginLeft={'1'}>Experimental</Box> |
| 94 | + </Box> |
| 95 | + </Text> |
| 96 | + </HStack> |
| 97 | + </ExperimentalFeatureTooltip> |
| 98 | + |
| 99 | + <IconButton |
| 100 | + size={'xs'} |
| 101 | + aria-label={'Close'} |
| 102 | + theme={'secondary'} |
| 103 | + onClick={() => setOpen(false)} |
| 104 | + > |
| 105 | + <CloseIcon /> |
| 106 | + </IconButton> |
| 107 | + </Box> |
| 108 | + |
| 109 | + <DynamicSizedContainer> |
| 110 | + <VStack spacing={2}> |
| 111 | + <FlexField> |
| 112 | + <SegmentedField<FontPickerModality> |
| 113 | + value={mode()} |
| 114 | + autoWidth |
| 115 | + onChange={item => setMode(item)} |
| 116 | + items={[ |
| 117 | + {label: 'Default', value: 'default'}, |
| 118 | + {label: 'System', value: 'system'}, |
| 119 | + ]} |
| 120 | + size={'sm'} |
| 121 | + /> |
| 122 | + </FlexField> |
| 123 | + |
| 124 | + <Switch> |
| 125 | + <Match when={mode() === 'default'}> |
| 126 | + <Listbox bordered {...webListboxProps} /> |
| 127 | + </Match> |
| 128 | + <Match when={mode() === 'system'}> |
| 129 | + <FontSystemPicker |
| 130 | + onEsc={() => setOpen(false)} |
| 131 | + value={props.value} |
| 132 | + onChange={value => props.onChange(value)} |
| 133 | + /> |
| 134 | + </Match> |
| 135 | + </Switch> |
| 136 | + </VStack> |
| 137 | + </DynamicSizedContainer> |
| 138 | + </PopoverContent> |
| 139 | + </Popover> |
| 140 | + ); |
| 141 | +} |
0 commit comments