-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Palette component #1304
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2d60f23
feat(EmojiPalette): added the EmojiPalette component (and EmojiContro…
Ruminat eded575
Merge remote-tracking branch 'origin/main' into feature/EmojiPalette-…
Ruminat 4bb82f4
fix: eslint
Ruminat a0e8be4
fix: turned EmojiPalette component into Palette
Ruminat 2e2ea6a
Merge remote-tracking branch 'origin/main' into feature/EmojiPalette-…
Ruminat 4932599
Merge remote-tracking branch 'origin/main' into feature/EmojiPalette-…
Ruminat 1a1d29e
Merge remote-tracking branch 'origin/main' into feature/EmojiPalette-…
Ruminat b94104c
Merge remote-tracking branch 'origin/main' into feature/EmojiPalette-…
Ruminat b2ddfd3
fix: god rid of the component, added property, added the hook
Ruminat 87b8a01
Merge remote-tracking branch 'origin/main' into feature/EmojiPalette-…
Ruminat e977699
fix: fixed the disabled state for Palette item
Ruminat b92c1ac
fix: pr fixes + added tests + used direction + removed iconClassName
Ruminat 54bdfeb
Merge remote-tracking branch 'origin/main' into feature/EmojiPalette-…
Ruminat 3951b49
Merge branch 'main' into feature/EmojiPalette-component
Ruminat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,35 @@ | ||
@use '../variables'; | ||
|
||
$block: '.#{variables.$ns}palette'; | ||
|
||
#{$block} { | ||
display: inline-flex; | ||
flex-flow: column wrap; | ||
gap: 8px; | ||
|
||
&:focus { | ||
border: none; | ||
outline: none; | ||
} | ||
|
||
&__row { | ||
display: inline-flex; | ||
gap: 8px; | ||
} | ||
|
||
&_size_xs &__option { | ||
font-size: 16px; | ||
} | ||
&_size_s &__option { | ||
font-size: 18px; | ||
} | ||
&_size_m &__option { | ||
font-size: 20px; | ||
} | ||
&_size_l &__option { | ||
font-size: 24px; | ||
} | ||
&_size_xl &__option { | ||
font-size: 28px; | ||
} | ||
} |
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,217 @@ | ||
import React from 'react'; | ||
|
||
import {useSelect} from '../../hooks'; | ||
import {useForkRef} from '../../hooks/useForkRef/useForkRef'; | ||
import type {ButtonProps} from '../Button'; | ||
import {Button} from '../Button'; | ||
import type {ControlGroupProps, DOMProps, QAProps} from '../types'; | ||
import {block} from '../utils/cn'; | ||
|
||
import {usePaletteGrid} from './hooks'; | ||
import {getPaletteRows} from './utils'; | ||
|
||
import './Palette.scss'; | ||
|
||
const b = block('palette'); | ||
|
||
export type PaletteOption = Pick<ButtonProps, 'disabled' | 'title'> & { | ||
/** | ||
* Option value, which you can use in state or send to back-end and so on. | ||
*/ | ||
value: string; | ||
/** | ||
* Content inside the option (emoji/image/GIF/symbol etc). | ||
* | ||
* Uses `value` as default, if `value` is a number, then it is treated as a unicode symbol (emoji for example). | ||
* | ||
* @default props.value | ||
*/ | ||
content?: React.ReactNode; | ||
}; | ||
|
||
export interface PaletteProps | ||
Ruminat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
extends Pick<ControlGroupProps, 'aria-label' | 'aria-labelledby'>, | ||
Pick<ButtonProps, 'disabled' | 'size'>, | ||
DOMProps, | ||
QAProps { | ||
/** | ||
* Allows selecting multiple options. | ||
* | ||
* @default true | ||
*/ | ||
multiple?: boolean; | ||
/** | ||
* Current value (which options are selected). | ||
*/ | ||
value?: string[]; | ||
/** | ||
* The control's default value. Use when the component is not controlled. | ||
*/ | ||
defaultValue?: string[]; | ||
/** | ||
* List of Palette options (the grid). | ||
*/ | ||
options?: PaletteOption[]; | ||
/** | ||
* How many options are there per row. | ||
* | ||
* @default 6 | ||
*/ | ||
columns?: number; | ||
/** | ||
* HTML class attribute for a grid row. | ||
*/ | ||
rowClassName?: string; | ||
/** | ||
* HTML class attribute for a grid option. | ||
*/ | ||
optionClassName?: string; | ||
/** | ||
* HTML class attribute for the `<Button.Icon />` inside a grid option. | ||
*/ | ||
iconClassName?: string; | ||
Ruminat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Fires when a user (un)selects an option. | ||
*/ | ||
onUpdate?: (value: string[]) => void; | ||
/** | ||
* Fires when a user focuses on the Palette. | ||
*/ | ||
onFocus?: (event: React.FocusEvent) => void; | ||
/** | ||
* Fires when a user blurs from the Palette. | ||
*/ | ||
onBlur?: (event: React.FocusEvent) => void; | ||
} | ||
|
||
interface PaletteComponent | ||
extends React.ForwardRefExoticComponent<PaletteProps & React.RefAttributes<HTMLDivElement>> {} | ||
|
||
export const Palette = React.forwardRef<HTMLDivElement, PaletteProps>(function Palette(props, ref) { | ||
const { | ||
size = 's', | ||
Ruminat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
multiple = true, | ||
options = [], | ||
columns = 6, | ||
disabled, | ||
style, | ||
className, | ||
rowClassName, | ||
optionClassName, | ||
iconClassName, | ||
qa, | ||
onFocus, | ||
onBlur, | ||
} = props; | ||
|
||
const [focusedOptionIndex, setFocusedOptionIndex] = React.useState<number | undefined>( | ||
undefined, | ||
); | ||
const focusedOption = | ||
focusedOptionIndex === undefined ? undefined : options[focusedOptionIndex]; | ||
|
||
const innerRef = React.useRef<HTMLDivElement>(null); | ||
const handleRef = useForkRef(ref, innerRef); | ||
|
||
const {value, handleSelection} = useSelect({ | ||
value: props.value, | ||
defaultValue: props.defaultValue, | ||
multiple, | ||
onUpdate: props.onUpdate, | ||
}); | ||
|
||
const rows = React.useMemo(() => getPaletteRows(options, columns), [columns, options]); | ||
|
||
const focusOnOptionWithIndex = React.useCallback((index: number) => { | ||
if (!innerRef.current) return; | ||
|
||
const $options = Array.from( | ||
innerRef.current.querySelectorAll(`.${b('option')}`), | ||
) as HTMLButtonElement[]; | ||
|
||
if (!$options[index]) return; | ||
|
||
$options[index].focus(); | ||
|
||
setFocusedOptionIndex(index); | ||
}, []); | ||
|
||
const tryToFocus = (newIndex: number) => { | ||
if (newIndex === focusedOptionIndex || newIndex < 0 || newIndex >= options.length) { | ||
return; | ||
} | ||
|
||
focusOnOptionWithIndex(newIndex); | ||
}; | ||
|
||
const gridProps = usePaletteGrid({ | ||
disabled, | ||
onFocus: (event) => { | ||
focusOnOptionWithIndex(0); | ||
onFocus?.(event); | ||
}, | ||
onBlur: (event) => { | ||
setFocusedOptionIndex(undefined); | ||
onBlur?.(event); | ||
}, | ||
whenFocused: | ||
focusedOptionIndex !== undefined && focusedOption | ||
? { | ||
selectItem: () => handleSelection(focusedOption), | ||
nextItem: () => tryToFocus(focusedOptionIndex + 1), | ||
previousItem: () => tryToFocus(focusedOptionIndex - 1), | ||
nextRow: () => tryToFocus(focusedOptionIndex + columns), | ||
previousRow: () => tryToFocus(focusedOptionIndex - columns), | ||
} | ||
: undefined, | ||
}); | ||
|
||
return ( | ||
<div | ||
Ruminat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{...gridProps} | ||
aria-label={props['aria-label']} | ||
aria-labelledby={props['aria-labelledby']} | ||
ref={handleRef} | ||
className={b({size}, className)} | ||
style={style} | ||
data-qa={qa} | ||
> | ||
{rows.map((row, rowNumber) => ( | ||
<div className={b('row', rowClassName)} key={`row-${rowNumber}`} role="row"> | ||
{row.map((option) => { | ||
const isSelected = Boolean(value.includes(option.value)); | ||
const focused = option === focusedOption; | ||
|
||
return ( | ||
<div | ||
key={option.value} | ||
role="gridcell" | ||
aria-selected={focused ? 'true' : undefined} | ||
aria-readonly={option.disabled} | ||
> | ||
<Button | ||
className={b('option', optionClassName)} | ||
tabIndex={-1} | ||
style={style} | ||
disabled={disabled || option.disabled} | ||
title={option.title} | ||
view={isSelected ? 'normal' : 'flat'} | ||
selected={isSelected} | ||
extraProps={{value: option.value}} | ||
size={size} | ||
onClick={() => handleSelection(option)} | ||
> | ||
<Button.Icon className={iconClassName}> | ||
{option.content ?? option.value} | ||
</Button.Icon> | ||
</Button> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}) as PaletteComponent; | ||
|
||
Palette.displayName = 'Palette'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need shift the sizes down, they're a bit bigger than they should be. You can check font sizes of Button
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shifted the font sizes down according to the
Button
's--_--icon-size
:12px
/16px
/16px
/16px
/20px
The font sizes of
Button
are basically the same (except for thexl
), so I didn't use them.