Skip to content

Commit

Permalink
Merge pull request #613 from tokens-studio/add-css-modules
Browse files Browse the repository at this point in the history
add css modules
  • Loading branch information
mck authored Jan 3, 2025
2 parents 4e2908b + f763f9a commit 0757c9b
Show file tree
Hide file tree
Showing 34 changed files with 505 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { DragItem } from './DragItem.js';
import { DropPanelStore } from './data.js';
import { NodeEntry } from './NodeEntry.js';
import { observer } from 'mobx-react-lite';
import { panelItemsSelector } from '@/redux/selectors/registry.js';
import {
panelItemsSelector,
previewItemsSelector,
} from '@/redux/selectors/registry.js';
import { useSelector } from 'react-redux';
import NavArrowRight from '@tokens-studio/icons/NavArrowRight.js';
import React, { useState } from 'react';
Expand All @@ -28,6 +31,12 @@ export const DropPanel = () => {
return <DropPanelInner data={data} />;
};

export const PreviewDropPanel = () => {
const data = useSelector(previewItemsSelector);
console.log(data);
return <DropPanelInner data={data} />;
};

export const DropPanelInner = observer(({ data }: IDropPanel) => {
const [search, setSearch] = React.useState('');
const [opened, setOpened] = useState<string[]>([]);
Expand Down
2 changes: 2 additions & 0 deletions packages/graph-editor/src/editor/editorTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export interface EditorProps {
*/
panelItems: DropPanelStore;

previewPanelItems: DropPanelStore;

/**
* Customize the controls that are displayed in the editor
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/graph-editor/src/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const Editor = React.forwardRef<ImperativeEditorRef, EditorProps>(
const {
panelItems = defaultPanelGroupsFactory(),
capabilities,
previewPanelItems,
toolbarButtons,
schemas,
nodeTypes = defaultNodeLookup,
Expand All @@ -30,6 +31,7 @@ export const Editor = React.forwardRef<ImperativeEditorRef, EditorProps>(
<ToastProvider>
<ReduxProvider
icons={icons}
previewPanelItems={previewPanelItems}
schemas={schemas}
controls={controls}
panelItems={panelItems}
Expand Down
23 changes: 22 additions & 1 deletion packages/graph-editor/src/editor/layoutController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { useDispatch } from '@/hooks/useDispatch.js';
import { useRegisterRef } from '@/hooks/useRegisterRef.js';
import { useSelector } from 'react-redux';

import { DropPanel } from '@/components/panels/dropPanel/dropPanel.js';
import {
DropPanel,
PreviewDropPanel,
} from '@/components/panels/dropPanel/dropPanel.js';
import { EditorProps, ImperativeEditorRef } from './editorTypes.js';
import { ErrorBoundary } from 'react-error-boundary';
import { ErrorBoundaryContent } from '@/components/ErrorBoundaryContent.js';
Expand Down Expand Up @@ -181,6 +184,11 @@ const layoutDataFactory = (props, ref): LayoutData => {
title: '',
content: <></>,
},
{
id: 'previewNodesPanel',
title: '',
content: <></>,
},
],
},
],
Expand Down Expand Up @@ -298,6 +306,19 @@ const layoutLoader = (tab: TabBase): TabData => {
closable: true,
};

case 'previewNodesPanel':
return {
group: 'popout',
id: 'previewNodesPanel',
title: 'Preview',
content: (
<ErrorBoundary fallback={<ErrorBoundaryContent />}>
<PreviewDropPanel />
</ErrorBoundary>
),
closable: true,
};

default:
return tab as TabData;
}
Expand Down
1 change: 1 addition & 0 deletions packages/graph-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from './registry/control.js';
export * from './registry/specifics.js';
export * from './registry/toolbar.js';
export * from './types/index.js';
export { DropPanelStore } from './components/panels/dropPanel/data.js';
7 changes: 7 additions & 0 deletions packages/graph-editor/src/redux/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ReduxProvider = ({
capabilities,
icons,
schemas,
previewPanelItems,
controls,
specifics,
toolbarButtons,
Expand All @@ -25,6 +26,12 @@ export const ReduxProvider = ({
}
}, [schemas]);

useEffect(() => {
if (previewPanelItems) {
store.dispatch.registry.setPreviewPanelItems(previewPanelItems);
}
}, [previewPanelItems]);

useEffect(() => {
store.dispatch.registry.registerIcons(icons || {});
}, [icons]);
Expand Down
8 changes: 8 additions & 0 deletions packages/graph-editor/src/redux/models/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface RegistryState {
controls: Control[];
nodeTypes: Record<string, typeof Node>;
panelItems: DropPanelStore;
previewItems: DropPanelStore;
capabilities: CapabilityFactory[];
toolbarButtons: ReactElement[];
schemas: SchemaObject[];
Expand All @@ -38,6 +39,7 @@ export const registryState = createModel<RootModel>()({
inputControls: { ...inputControls },
controls: [...(defaultControls as Control[])],
panelItems: defaultPanelGroupsFactory(),
previewItems: new DropPanelStore([]),
nodeTypes: {},
capabilities: [],
toolbarButtons: DefaultToolbarButtons(),
Expand Down Expand Up @@ -110,5 +112,11 @@ export const registryState = createModel<RootModel>()({
panelItems: payload,
};
},
setPreviewPanelItems(state, payload: DropPanelStore) {
return {
...state,
previewItems: payload,
};
},
},
});
5 changes: 5 additions & 0 deletions packages/graph-editor/src/redux/selectors/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export const panelItemsSelector = createSelector(
(state) => state.panelItems,
);

export const previewItemsSelector = createSelector(
registry,
(state) => state.previewItems,
);

export const capabilitiesSelector = createSelector(
registry,
(state) => state.capabilities,
Expand Down
30 changes: 21 additions & 9 deletions packages/nodes-previews/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"access": "public"
},
"scripts": {
"build": "tspc -p tsconfig.prod.json",
"dev": "tspc -p tsconfig.prod.json --watch",
"build": "tspc -p tsconfig.prod.json && postcss \"src/**/*.css\" --base src --dir dist",
"dev": "tspc -p tsconfig.prod.json --watch",
"docs": "typedoc",
"format": "npm run format:eslint && npm run format:prettier",
"format:eslint": "eslint . --fix",
Expand All @@ -34,27 +34,39 @@
"mobx-react-lite": "^4.0.5"
},
"peerDependencies": {
"@tokens-studio/icons": "^0.1.4",
"@tokens-studio/ui": "^1.0.7",
"@tokens-studio/graph-editor": "*",
"@tokens-studio/graph-engine": "*",
"react": "^18.2.0",
"colorjs.io": "^0.5.2"
"@tokens-studio/graph-engine-nodes-design-tokens": "*",
"@tokens-studio/icons": "^0.1.4",
"@tokens-studio/ui": "^1.0.7",
"colorjs.io": "^0.5.2",
"react": "^18.2.0"
},
"devDependencies": {
"@changesets/cli": "^2.26.0",
"@testing-library/react": "^16.1.0",
"@tokens-studio/eslint-config-custom": "*",
"@tokens-studio/prettier-config-custom": "*",
"@tokens-studio/graph-editor": "*",
"@tokens-studio/graph-engine": "*",
"typescript-transform-paths": "^3.5.1",
"@tokens-studio/graph-engine-nodes-design-tokens": "*",
"@tokens-studio/prettier-config-custom": "*",
"autoprefixer": "^10.4.20",
"cssnano": "^7.0.6",
"postcss": "^8.4.47",
"postcss-cli": "^11.0.0",
"postcss-css-variables": "^0.19.0",
"postcss-import": "^16.1.0",
"postcss-modules": "^6.0.0",
"postcss-nested": "^6.2.0",
"postcss-preset-env": "^10.0.5",
"typedoc": "^0.24.7",
"typescript": "^5.4.5",
"typescript-transform-paths": "^3.5.1",
"vitest": "^1.6.0"
},
"keywords": [
"studio",
"tokens",
"ui"
]
}
}
20 changes: 20 additions & 0 deletions packages/nodes-previews/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import postcssImport from 'postcss-import';
import postcssNested from 'postcss-nested';
import postcssPresetEnv from 'postcss-preset-env';

export default {
plugins: [
postcssImport,
postcssNested,
postcssPresetEnv({
stage: 3,
features: {
'nesting-rules': true,
'custom-properties': true,
'custom-media-queries': true,
'media-query-ranges': true,
'custom-selectors': true
}
})
]
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ColorSchema, Node } from '@tokens-studio/graph-engine';
import { arrayOf } from '../utils/index.js';
import { arrayOf } from '../../utils/index.js';

export default class NodeDefinition extends Node {
static title = 'Color Scale';
static type = 'studio.tokens.previews.colorScale';
static type = 'studio.tokens.previews.color.scale';

static description = 'Previews a color scale';

Expand Down
16 changes: 16 additions & 0 deletions packages/nodes-previews/src/color/scale/panel.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.scale {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: scroll;
height: 100%;
}

.swatch {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
min-height: 80px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Text } from '@tokens-studio/ui/Text.js';
import { castToHex } from '../../utils/index.js';
import { observer } from 'mobx-react-lite';
import Color from 'colorjs.io';
import ColorScale from '../../nodes/colorScale.js';
import React from 'react';
import styles from './panel.module.css';
import type ColorScale from './node.js';

function contrastingColor(value: string) {
const black = new Color('srgb', [0, 0, 0]);
Expand All @@ -23,36 +24,23 @@ function contrastingColor(value: string) {
export const ColorScalePreview = observer(
({ inputs }: { inputs: ColorScale['inputs'] }) => {
return (
<>
<div className={styles.scale}>
{inputs.scale && (
<>
{inputs.scale.value.map(color => {
const hex = castToHex(color);

return (
<div
style={{
display: 'grid',
placeItems: 'center',
width: '100%',
minHeight: '100px',
backgroundColor: hex
}}
>
<Text
style={{
font: 'var(--font-body-xl)',
color: contrastingColor(hex)
}}
>
<div className={styles.swatch} style={{ backgroundColor: hex }}>
<Text size='large' style={{ color: contrastingColor(hex) }}>
{hex}
</Text>
</div>
);
})}
</>
)}
</>
</div>
);
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { IconButton } from '@tokens-studio/ui/IconButton.js';
import { Button } from '@tokens-studio/ui/Button.js';
import { useOpenPanel } from '@tokens-studio/graph-editor';
import ColorScalePreview from '../panels/colorScale.js';
import ColorScalePreview from './panel.js';
import Eye from '@tokens-studio/icons/Eye.js';
import React from 'react';

const ColorScale = ({ node }) => {
export const ColorScale = ({ node }) => {
const { toggle } = useOpenPanel();

return (
<IconButton
<Button
fullWidth
emphasis='high'
onClick={() => {
toggle({
group: 'popout',
Expand All @@ -18,11 +20,8 @@ const ColorScale = ({ node }) => {
});
}}
icon={<Eye />}
tooltip='Toggle Preview'
/>
>
Toggle Preview
</Button>
);
};

export const specifics = {
'studio.tokens.previews.colorScale': ColorScale
};
15 changes: 15 additions & 0 deletions packages/nodes-previews/src/color/swatch/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ColorSchema, Node } from '@tokens-studio/graph-engine';

export default class NodeDefinition extends Node {
static title = 'Color Swatch';
static type = 'studio.tokens.previews.color.swatch';
static description = 'Previews a single color';

constructor(props) {
super(props);

this.addInput('color', {
type: ColorSchema
});
}
}
Loading

0 comments on commit 0757c9b

Please sign in to comment.