-
Notifications
You must be signed in to change notification settings - Fork 50
feat: add fontFamily input #548
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| .container { | ||
| /* align-items: start; */ | ||
| display: grid; | ||
| gap: 1rem; | ||
|
|
||
| /* contain: inline-size; */ | ||
|
|
||
| /* justify-content: start; */ | ||
| width: 28rem; | ||
| } | ||
|
|
||
| .item { | ||
| align-items: center; | ||
| display: flex; | ||
| gap: 0.25rem; | ||
| } | ||
|
|
||
| .list { | ||
| /* align-items: start; */ | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 0.5rem; | ||
| justify-content: stretch; | ||
| list-style: none; | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
|
|
||
| .removeButton { | ||
| padding: 0.5em; | ||
| } | ||
|
|
||
| .input { | ||
| flex: 1; | ||
| } | ||
|
|
||
| .input input { | ||
| width: auto; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| declare const classNames: { | ||
| readonly container: 'container'; | ||
| readonly item: 'item'; | ||
| readonly list: 'list'; | ||
| readonly removeButton: 'removeButton'; | ||
| readonly input: 'input'; | ||
| }; | ||
| export default classNames; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { move } from '@dnd-kit/helpers'; | ||
| import { DragDropProvider } from '@dnd-kit/react'; | ||
| import { useSortable } from '@dnd-kit/react/sortable'; | ||
| import { CaretSort, Cross } from '@terrazzo/icons'; | ||
| import { Button, SubtleInput } from '@terrazzo/tiles'; | ||
| import type { FontFamilyTokenNormalized } from '@terrazzo/token-tools'; | ||
| import { useReducer } from 'react'; | ||
| import c from './EditableFontFamilyToken.module.css'; | ||
|
|
||
| type SortableProps = { | ||
| id: string | number; | ||
| index: number; | ||
| children?: React.ReactNode; | ||
| }; | ||
|
|
||
| function Sortable({ id, index, children }: SortableProps) { | ||
| const { ref } = useSortable({ id, index }); | ||
|
|
||
| return ( | ||
| <li ref={ref} className={c.item}> | ||
| <CaretSort /> | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: add drag handle icon |
||
| {children} | ||
| </li> | ||
| ); | ||
| } | ||
|
|
||
| // function SortableList() { | ||
| // const items = [1, 2, 3, 4]; | ||
|
|
||
| // return ( | ||
| // <ul className={c.list}> | ||
| // {items.map((id, index) => ( | ||
| // <Sortable key={id} id={id} index={index} /> | ||
| // ))} | ||
| // </ul> | ||
| // ); | ||
| // } | ||
|
Comment on lines
+16
to
+37
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: pull sortable out into a reusable component
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: adjust behavior of sorting so that items don't move until dropped. This is probably deferrable since it's a UX issue. There are other higher priority concerns. Here's a demo of the desirable behavior though: https://shadcn-drag-and-drop.vercel.app/ |
||
|
|
||
| export interface EditableFontFamilyTokenProps { | ||
| id: string; | ||
| mode?: string; | ||
| value: FontFamilyTokenNormalized; | ||
| } | ||
|
|
||
| type FontFamilyItem = { | ||
| id: string; | ||
| value: string; | ||
| }; | ||
|
|
||
| type FontFamilyState = FontFamilyItem[]; | ||
|
|
||
| const ADD_FONT_FAMILY = 'ADD_FONT_FAMILY'; | ||
| const REMOVE_FONT_FAMILY = 'REMOVE_FONT_FAMILY'; | ||
| const UPDATE_FONT_FAMILY = 'UPDATE_FONT_FAMILY'; | ||
| const INITIALIZE = 'INITIALIZE'; | ||
| const UPDATE_FONT_FAMILY_LIST = 'UPDATE_FONT_FAMILY_LIST'; | ||
|
|
||
| type FontFamilyAction = | ||
| | { type: typeof ADD_FONT_FAMILY; payload: { id: string } } | ||
| | { type: typeof REMOVE_FONT_FAMILY; payload: { id: string } } | ||
| | { type: typeof UPDATE_FONT_FAMILY; payload: { id: string; value: string } } | ||
| | { type: typeof INITIALIZE; payload: FontFamilyItem[] } | ||
| | { type: typeof UPDATE_FONT_FAMILY_LIST; payload: FontFamilyItem[] }; | ||
|
|
||
| const generateFontFamilyList = (value: FontFamilyTokenNormalized): FontFamilyItem[] => { | ||
| return value.$value.map((fontName, index) => ({ | ||
| id: `font-${index}`, | ||
| value: fontName, | ||
| })); | ||
| }; | ||
|
|
||
| const fontFamilyReducer = (state: FontFamilyState, action: FontFamilyAction): FontFamilyState => { | ||
| switch (action.type) { | ||
| case INITIALIZE: | ||
| return action.payload; | ||
| case ADD_FONT_FAMILY: | ||
| return [...state, { id: action.payload.id, value: '' }]; | ||
| case REMOVE_FONT_FAMILY: | ||
| return state.filter((item) => item.id !== action.payload.id); | ||
| case UPDATE_FONT_FAMILY: | ||
| return state.map((item) => (item.id === action.payload.id ? { ...item, value: action.payload.value } : item)); | ||
| case UPDATE_FONT_FAMILY_LIST: | ||
| return action.payload; | ||
| default: | ||
| return state; | ||
| } | ||
| }; | ||
|
|
||
| const useFontFamily = (value: FontFamilyTokenNormalized) => { | ||
| const defaultFontFamilyList = generateFontFamilyList(value); | ||
| const [fontFamilyList, dispatch] = useReducer(fontFamilyReducer, defaultFontFamilyList); | ||
|
|
||
| const setFontFamilyList = (list: FontFamilyItem[]) => { | ||
| dispatch({ type: INITIALIZE, payload: list }); | ||
| }; | ||
|
|
||
| const addFontFamily = () => { | ||
| const newId = `font-${fontFamilyList.length}`; | ||
| dispatch({ type: ADD_FONT_FAMILY, payload: { id: newId } }); | ||
| }; | ||
|
|
||
| const removeFontFamily = (id: string) => { | ||
| dispatch({ type: REMOVE_FONT_FAMILY, payload: { id } }); | ||
| }; | ||
|
|
||
| const updateFontFamily = (id: string, value: string) => { | ||
| dispatch({ type: UPDATE_FONT_FAMILY, payload: { id, value } }); | ||
| }; | ||
|
|
||
| return { | ||
| fontFamilyList, | ||
| setFontFamilyList, | ||
| addFontFamily, | ||
| removeFontFamily, | ||
| updateFontFamily, | ||
| }; | ||
| }; | ||
|
|
||
| export default function EditableFontFamilyToken({ value }: EditableFontFamilyTokenProps) { | ||
| const { fontFamilyList, setFontFamilyList, addFontFamily, removeFontFamily, updateFontFamily } = useFontFamily(value); | ||
|
|
||
| return ( | ||
| <div className={c.container}> | ||
| <DragDropProvider | ||
| onDragEnd={(event) => { | ||
| setFontFamilyList(move(fontFamilyList, event)); | ||
| }} | ||
| > | ||
| <ul className={c.list}> | ||
| {fontFamilyList.map((item, index) => ( | ||
| <Sortable key={item.id} id={item.id} index={index}> | ||
| <SubtleInput | ||
| className={c.input} | ||
| value={item.value} | ||
| type='text' | ||
| onChange={(e) => updateFontFamily(item.id, e.target.value)} | ||
| placeholder='Font Family...' | ||
| /> | ||
| <Button | ||
| className={c.removeButton} | ||
| aria-label='Remove font family' | ||
| type='button' | ||
| onClick={() => removeFontFamily(item.id)} | ||
| size='s' | ||
| variant='orange' | ||
| > | ||
| <Cross /> | ||
| </Button> | ||
| </Sortable> | ||
| ))} | ||
| </ul> | ||
| </DragDropProvider> | ||
| <div> | ||
| <Button type='button' onClick={addFontFamily} size='s'> | ||
| + Add Font Family | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: add plus icon |
||
| </Button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
@drwpow I have this as a draft PR because I'd like to get your thoughts on using this package before going further. Are there alternatives we should explore instead?
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.
Nah go for it. For the token lab we can add anything here. DND is a great library (used it a little, liked it)