-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various improvements across dialogs Fixes #105
- Loading branch information
Showing
13 changed files
with
438 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useCombobox } from 'downshift' | ||
import React from 'react' | ||
import { Control, UseFormSetValue, Controller } from 'react-hook-form' | ||
import styles from '../../../styles/ui.module.css' | ||
import DropDownIcon from '../../../icons/arrow_drop_down.svg' | ||
|
||
const MAX_SUGGESTIONS = 20 | ||
|
||
export const DownshiftAutoComplete: React.FC<{ | ||
suggestions: string[] | ||
control: Control<any, any> | ||
setValue: UseFormSetValue<any> | ||
placeholder: string | ||
inputName: string | ||
autofocus?: boolean | ||
initialInputValue: string | ||
}> = ({ autofocus, suggestions, control, inputName, placeholder, initialInputValue, setValue }) => { | ||
const [items, setItems] = React.useState(suggestions.slice(0, MAX_SUGGESTIONS)) | ||
|
||
const enableAutoComplete = suggestions.length > 0 | ||
|
||
const { isOpen, getToggleButtonProps, getMenuProps, getInputProps, highlightedIndex, getItemProps, selectedItem } = useCombobox({ | ||
initialInputValue, | ||
onInputValueChange({ inputValue }) { | ||
inputValue = inputValue?.toLowerCase() || '' | ||
setValue(inputName, inputValue) | ||
const matchingItems = [] | ||
for (const suggestion of suggestions) { | ||
if (suggestion.toLowerCase().includes(inputValue)) { | ||
matchingItems.push(suggestion) | ||
if (matchingItems.length >= MAX_SUGGESTIONS) { | ||
break | ||
} | ||
} | ||
} | ||
setItems(matchingItems) | ||
}, | ||
items, | ||
itemToString(item) { | ||
return item ?? '' | ||
} | ||
}) | ||
|
||
const dropdownIsVisible = isOpen && items.length > 0 | ||
return ( | ||
<div className={styles.downshiftAutocompleteContainer}> | ||
<div data-visible-dropdown={dropdownIsVisible} className={styles.downshiftInputWrapper}> | ||
<Controller | ||
name={inputName} | ||
control={control} | ||
render={({ field }) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const downshiftSrcProps = getInputProps() | ||
return ( | ||
<input | ||
{...downshiftSrcProps} | ||
name={field.name} | ||
placeholder={placeholder} | ||
className={styles.downshiftInput} | ||
size={30} | ||
data-editor-dialog={true} | ||
autoFocus={autofocus} | ||
/> | ||
) | ||
}} | ||
/> | ||
{enableAutoComplete && ( | ||
<button aria-label="toggle menu" type="button" {...getToggleButtonProps()}> | ||
<DropDownIcon /> | ||
</button> | ||
)} | ||
</div> | ||
|
||
<div className={styles.downshiftAutocompleteContainer}> | ||
<ul {...getMenuProps()} data-visible={dropdownIsVisible}> | ||
{items.map((item, index: number) => ( | ||
<li | ||
data-selected={selectedItem === item} | ||
data-highlighted={highlightedIndex === index} | ||
key={`${item}${index}`} | ||
{...getItemProps({ item, index })} | ||
> | ||
{item} | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import * as Dialog from '@radix-ui/react-dialog' | ||
import classNames from 'classnames' | ||
import React from 'react' | ||
import { useForm } from 'react-hook-form' | ||
import styles from '../../styles/ui.module.css' | ||
import { corePluginHooks } from '../core/index' | ||
import { imagePluginHooks } from './index' | ||
import { DownshiftAutoComplete } from '../core/ui/DownshiftAutoComplete' | ||
|
||
interface ImageFormFields { | ||
src: string | ||
title: string | ||
altText: string | ||
file: FileList | ||
} | ||
|
||
export const ImageDialog: React.FC = () => { | ||
const [imageAutocompleteSuggestions, state] = imagePluginHooks.useEmitterValues('imageAutocompleteSuggestions', 'imageDialogState') | ||
const saveImage = imagePluginHooks.usePublisher('saveImage') | ||
const [editorRootElementRef] = corePluginHooks.useEmitterValues('editorRootElementRef') | ||
const closeImageDialog = imagePluginHooks.usePublisher('closeImageDialog') | ||
|
||
const { register, handleSubmit, control, setValue, reset } = useForm<ImageFormFields>({ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
values: state.type === 'editing' ? (state.initialValues as any) : {} | ||
}) | ||
|
||
return ( | ||
<Dialog.Root | ||
open={state.type !== 'inactive'} | ||
onOpenChange={(open) => { | ||
if (!open) { | ||
closeImageDialog(true) | ||
reset({ src: '', title: '', altText: '' }) | ||
} | ||
}} | ||
> | ||
<Dialog.Portal container={editorRootElementRef?.current}> | ||
<Dialog.Overlay className={styles.dialogOverlay} /> | ||
<Dialog.Content | ||
className={styles.dialogContent} | ||
onOpenAutoFocus={(e) => { | ||
e.preventDefault() | ||
}} | ||
> | ||
<form | ||
onSubmit={(e) => { | ||
void handleSubmit(saveImage)(e) | ||
reset({ src: '', title: '', altText: '' }) | ||
e.nativeEvent.stopImmediatePropagation() | ||
}} | ||
className={styles.multiFieldForm} | ||
> | ||
<div className={styles.formField}> | ||
<label htmlFor="file">Upload an image from your device:</label> | ||
<input type="file" {...register('file')} /> | ||
</div> | ||
|
||
<div className={styles.formField}> | ||
<label htmlFor="src">Or add an image from an URL:</label> | ||
<DownshiftAutoComplete | ||
initialInputValue={state.type === 'editing' ? state.initialValues.src || '' : ''} | ||
inputName="src" | ||
suggestions={imageAutocompleteSuggestions} | ||
setValue={setValue} | ||
control={control} | ||
placeholder="Select or paste an image src" | ||
/> | ||
</div> | ||
|
||
<div className={styles.formField}> | ||
<label htmlFor="alt">Alt:</label> | ||
<input type="text" {...register('altText')} className={styles.textInput} /> | ||
</div> | ||
|
||
<div className={styles.formField}> | ||
<label htmlFor="title">Title:</label> | ||
<input type="text" {...register('title')} className={styles.textInput} /> | ||
</div> | ||
|
||
<div style={{ display: 'flex', justifyContent: 'flex-end', gap: 'var(--spacing-2)' }}> | ||
<button type="submit" title="Save" aria-label="Save" className={classNames(styles.primaryButton)}> | ||
Save | ||
</button> | ||
<Dialog.Close asChild> | ||
<button type="reset" title="Cancel" aria-label="Cancel" className={classNames(styles.secondaryButton)}> | ||
Cancel | ||
</button> | ||
</Dialog.Close> | ||
</div> | ||
</form> | ||
</Dialog.Content> | ||
</Dialog.Portal> | ||
</Dialog.Root> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.