Skip to content

Commit

Permalink
Merge branch 'acherkashin-dev/specify-tabs' into v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Sqrrl committed Feb 7, 2024
2 parents 5bee027 + 42ed8c6 commit fe46d90
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 84 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Display design token documentation generated from your stylesheets and icon file
- [Available presenters](#available-presenters)
- [Advanced configuration](#advanced-configuration)
- [Default tab](#default-tab)
- [Visible tabs](#visible-tabs)
- [Style injection](#style-injection)
- [Disable the addon panel](#disable-the-addon-panel)
- [Token search visibility](#token-search-visibility)
Expand Down Expand Up @@ -120,6 +121,20 @@ export default {
};
```

### Visible tabs

If you don't want to show all available tabs, it is possible to specify which tabs should be shown in the addon panel via the `tabs` setting.

```javascript
export default {
parameters: {
designToken: {
tabs: ['Colors']
}
}
}
```

### Style injection

To inject styles needed by your design token documentation, use the `styleInjection` parameter. A typical usecase are web fonts needed by your font family tokens. Please note that the styleInjection parameter only works with valid css.
Expand Down
6 changes: 6 additions & 0 deletions addon/src/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ export const Panel: React.FC<PanelProps> = (props) => {
tabs,
} = useTokenTabs(config);

// React shows a warning in the console when the count of tabs is changed because identifiers of tabs are used in the dependency array of the useMemo hook.
// To prevent this, we fully re-render the Tabs control by providing a new key when tabs are changed.
// https://github.com/storybookjs/storybook/blob/176017d03224f8d0b4add227ebf29a3705f994f5/code/ui/components/src/components/tabs/tabs.tsx#L151
const key = (tabs ?? []).map(item => item.label).join('-');

return (
<AddonPanel {...props}>
<>
<style>{styleInjections}</style>

<ScrollArea vertical horizontal>
<Tabs
key={key}
actions={{ onSelect: (id) => setActiveCategory(id) }}
selected={activeCategory}
>
Expand Down
59 changes: 0 additions & 59 deletions addon/src/components/Panel.tsx

This file was deleted.

36 changes: 12 additions & 24 deletions addon/src/hooks/useTokenTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,25 @@ export function useTokenTabs(config?: Config) {
new Set(categories.map((category) => category?.name))
);

return categoryNames.map((name) => ({
let tabs = categoryNames.map((name) => ({
label: name,
categories: categories.filter(
(category) => category?.name === name
) as Category[],
}));

if ((config?.tabs ?? []).length !== 0) {
tabs = tabs.filter(tab => config.tabs.includes(tab.label))
}

return tabs;
}, [
cssCategories,
lessCategories,
scssCategories,
svgIconCategories,
imageCategories,
config
]);

useEffect(() => {
Expand All @@ -76,37 +83,16 @@ export function useTokenTabs(config?: Config) {

if (cssTokens) {
setCssCategories(cssTokens.categories);

if (!config?.defaultTab && cssTokens.categories.length > 0) {
setActiveCategory(
(activeCategory) => activeCategory || cssTokens.categories[0].name
);
}

setStyleInjections((current) => current + cssTokens.injectionStyles);
}

if (lessTokens) {
setLessCategories(lessTokens.categories);

if (!config?.defaultTab && lessTokens.categories.length > 0) {
setActiveCategory(
(activeCategory) => activeCategory || lessTokens.categories[0].name
);
}

setStyleInjections((current) => current + lessTokens.injectionStyles);
}

if (scssTokens) {
setScssCategories(scssTokens.categories);

if (!config?.defaultTab && scssTokens.categories.length > 0) {
setActiveCategory(
(activeCategory) => activeCategory || scssTokens.categories[0].name
);
}

setStyleInjections((current) => current + scssTokens.injectionStyles);
}

Expand All @@ -120,10 +106,12 @@ export function useTokenTabs(config?: Config) {
}, [config, tokenFiles]);

useEffect(() => {
if (config?.defaultTab) {
if (config?.defaultTab && tabs.find(item => item.label === config.defaultTab)) {
setActiveCategory(config.defaultTab);
} else if (tabs.length > 0) {
setActiveCategory(tabs[0].label);
}
}, [config]);
}, [config, tabs]);

return {
activeCategory,
Expand Down
11 changes: 10 additions & 1 deletion addon/src/stories/Button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,14 @@ export const Primary: Story = {
args: {
primary: true,
label: "Button",
},
},
};

export const ColorsOnly: Story = {
args: { ...Primary.args },
parameters: {
designToken: {
tabs: ['Colors']
}
}
}
1 change: 1 addition & 0 deletions addon/src/types/config.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Config {
styleInjection?: string;
pageSize?: number;
presenters?: PresenterMapType;
tabs?: string[];
}

export interface File {
Expand Down

0 comments on commit fe46d90

Please sign in to comment.