Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/token-lab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"react-dom": "^19.0.0"
},
"dependencies": {
"@dnd-kit/helpers": "^0.1.21",
"@dnd-kit/react": "^0.1.21",
"@radix-ui/react-popover": "^1.1.15",
"@terrazzo/fonts": "workspace:^",
"@terrazzo/icons": "workspace:^",
Expand Down
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';
Comment on lines +1 to +3
Copy link
Collaborator Author

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?

Copy link
Collaborator

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)

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 />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: pull sortable out into a reusable component

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add plus icon

</Button>
</div>
</div>
);
}
15 changes: 15 additions & 0 deletions packages/token-lab/src/components/EditableToken/EditableToken.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TokenNormalized } from '@terrazzo/token-tools';
import clsx from 'clsx';
import EditableColorToken from '../EditableColorToken/EditableColorToken.js';
import EditableFontFamilyToken from '../EditableFontFamilyToken/EditableFontFamilyToken.js';
import c from './EditableToken.module.css';

export interface TokenProps {
Expand All @@ -27,6 +28,20 @@ export default function EditableToken({ token, modes = ['.'] }: TokenProps) {
</tr>
);
}
case 'fontFamily': {
return (
<tr>
<th scope='row' className={clsx(c.cell)}>
{localName}
</th>
{modes.map((mode) => (
<td key={mode} className={clsx(c.cell)}>
<EditableFontFamilyToken id={token.id} mode={mode} value={token.mode[mode]!} />
</td>
))}
</tr>
);
}
default: {
return (
<tr>
Expand Down
79 changes: 79 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading