Skip to content
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

UI: Add URL parameters to SB to tweak visible UI #17891

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 45 additions & 2 deletions lib/ui/src/components/preview/preview.stories.tsx
Expand Up @@ -131,9 +131,52 @@ export const HideAllDefaultTools = () => (
export const WithCanvasTab = () => (
<Consumer>
{({ api }: Combo) => {
return <Preview {...previewProps} api={{ ...api, getElements: () => ({}) }} />;
return (
<Preview
{...previewProps}
api={{
...api,
getElements: () => ({}),
}}
/>
);
}}
</Consumer>
);

export const WithTabs = () => (
<Consumer>
{({ api }: Combo) => {
return (
<Preview
{...previewProps}
api={{
...api,
getElements: () => ({}),
getQueryParam: () => '',
}}
/>
);
}}
</Consumer>
);

export const WithTabs = () => <Preview {...previewProps} />;
export const WithToolbarExclusions = () => (
<Consumer>
{({ api }: Combo) => {
return (
<Preview
{...previewProps}
api={{
...api,
getElements: () => ({}),
getQueryParam: (key) => {
const params = { toolbarExclude: 'canvas,fullscreen' };
return params[key];
},
}}
/>
);
}}
</Consumer>
);
40 changes: 35 additions & 5 deletions lib/ui/src/components/preview/toolbar.tsx
Expand Up @@ -18,6 +18,8 @@ import { ejectTool } from './tools/eject';
import { menuTool } from './tools/menu';
import { addonsTool } from './tools/addons';

const TOOLBAR_EXCLUSION_PARAM = 'toolbarExclude';

export const getTools = (getFn: API['getElements']) => Object.values(getFn<Addon>(types.TOOL));

export const getToolsExtra = (getFn: API['getElements']) =>
Expand Down Expand Up @@ -115,7 +117,8 @@ const useTools = (
viewMode: PreviewProps['viewMode'],
story: PreviewProps['story'],
location: PreviewProps['location'],
path: PreviewProps['path']
path: PreviewProps['path'],
getQueryParam: API['getQueryParam']
) => {
const toolsFromConfig = useMemo(() => getTools(getElements), [getElements]);
const toolsExtraFromConfig = useMemo(() => getToolsExtra(getElements), [getElements]);
Expand All @@ -129,9 +132,19 @@ const useTools = (
[defaultToolsExtra, toolsExtraFromConfig]
);

const toolbarExclusions = getQueryParam(TOOLBAR_EXCLUSION_PARAM)
? getQueryParam(TOOLBAR_EXCLUSION_PARAM).split(',')
: [];

return useMemo(() => {
return story && story.parameters
? filterTools(tools, toolsExtra, tabs, { viewMode, story, location, path })
? filterTools(tools, toolsExtra, tabs, {
viewMode,
story,
location,
path,
toolbarExclusions,
})
: { left: tools, right: toolsExtra };
}, [viewMode, story, location, path, tools, toolsExtra, tabs]);
};
Expand All @@ -145,7 +158,15 @@ export interface ToolData {

export const ToolRes: FunctionComponent<ToolData & RenderData> = React.memo<ToolData & RenderData>(
({ api, story, tabs, isShown, location, path, viewMode }) => {
const { left, right } = useTools(api.getElements, tabs, viewMode, story, location, path);
const { left, right } = useTools(
api.getElements,
tabs,
viewMode,
story,
location,
path,
api.getQueryParam
);

return left || right ? (
<Toolbar key="toolbar" shown={isShown} border>
Expand Down Expand Up @@ -190,16 +211,24 @@ export function filterTools(
story,
location,
path,
toolbarExclusions,
}: {
viewMode: State['viewMode'];
story: PreviewProps['story'];
location: State['location'];
path: State['path'];
toolbarExclusions: string[];
}
) {
const isToolIncluded = (id: string) =>
toolbarExclusions.filter((exclusionKey) => id.match(new RegExp(`^${exclusionKey}.*`)))
.length === 0;

const filteredTabs = tabs.filter((tab) => isToolIncluded(tab.id));

const toolsLeft = [
menuTool,
tabs.filter((p) => !p.hidden).length >= 1 && createTabsTool(tabs),
filteredTabs.filter((p) => !p.hidden).length >= 1 && createTabsTool(filteredTabs),
...tools,
];
const toolsRight = [...toolsExtra];
Expand All @@ -214,7 +243,8 @@ export function filterTools(
location,
path,
})) &&
!toolbarItemHasBeenExcluded(item, story);
!toolbarItemHasBeenExcluded(item, story) &&
isToolIncluded(item.id);

const left = toolsLeft.filter(filter);
const right = toolsRight.filter(filter);
Expand Down