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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSF: Allow overridding globals at the story level #26654
base: next
Are you sure you want to change the base?
Changes from all commits
2a3815f
f243993
866f9ce
759cfd5
1ae58e5
16fc312
89e8456
6539f37
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import type { FC } from 'react'; | |
import React, { useState, useCallback } from 'react'; | ||
import { useGlobals } from '@storybook/manager-api'; | ||
import { WithTooltip, TooltipLinkList } from '@storybook/components'; | ||
import { dequal as deepEqual } from 'dequal'; | ||
import { ToolbarMenuButton } from './ToolbarMenuButton'; | ||
import type { WithKeyboardCycleProps } from '../hoc/withKeyboardCycle'; | ||
import { withKeyboardCycle } from '../hoc/withKeyboardCycle'; | ||
|
@@ -18,11 +19,12 @@ export const ToolbarMenuList: FC<ToolbarMenuListProps> = withKeyboardCycle( | |
description, | ||
toolbar: { icon: _icon, items, title: _title, preventDynamicIcon, dynamicTitle }, | ||
}) => { | ||
const [globals, updateGlobals] = useGlobals(); | ||
const [globals, updateGlobals, userGlobals] = useGlobals(); | ||
const [isTooltipVisible, setIsTooltipVisible] = useState(false); | ||
|
||
const currentValue = globals[id]; | ||
const hasGlobalValue = !!currentValue; | ||
const isOverridden = !deepEqual(currentValue, userGlobals[id]); | ||
let icon = _icon; | ||
let title = _title; | ||
|
||
|
@@ -42,7 +44,7 @@ export const ToolbarMenuList: FC<ToolbarMenuListProps> = withKeyboardCycle( | |
(value: string | undefined) => { | ||
updateGlobals({ [id]: value }); | ||
}, | ||
[currentValue, updateGlobals] | ||
[id, updateGlobals] | ||
); | ||
|
||
return ( | ||
|
@@ -79,6 +81,7 @@ export const ToolbarMenuList: FC<ToolbarMenuListProps> = withKeyboardCycle( | |
> | ||
<ToolbarMenuButton | ||
active={isTooltipVisible || hasGlobalValue} | ||
disabled={isOverridden} | ||
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. Readonly might be a better UX. See this recent PR 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. Can we leave it to someone else (w/ @cdedreuille's help) to create a readonly version of the |
||
description={description || ''} | ||
icon={icon} | ||
title={title || ''} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,9 @@ export default { | |
}; | ||
|
||
export const Basic = {}; | ||
|
||
export const Override = { | ||
globalOverrides: { | ||
locale: 'kr', | ||
}, | ||
}; |
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.
a deep equal check might be problematic for cases where functions or class instances are assigned to global values, leading to potential circular references.
Why can't we check whether
userGlobals[id]
is present? This matches my expectation of an "override". Otherwise, we should also rename the variable to something likeisDeepEqual
.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.
@valentinpalkovic:
globals need to be serializable to go over the channel, so I don't think your examples are possible. But there may be others we should consider.
So I think you have this backwards (this might be a sign that the naming is bad) -
userGlobals
are the globals the user has specified via clicking toolbars etc.globals = { ...userGlobals, ...globalOverrides }
. So this is how we check if aglobalOverride
is set for a key (doesglobals[key] == userGlobals[key]
).Now I think about it, maybe we've made it difficult to do the thing that you generally would want to do (know if a value has been overrdden). Perhaps instead we should set
{ globals, globalOverrides }
on the event, or even{ globals, globalOverrides, userGlobals }
?WDYT @shilman? The downside is quite a bit of redundancy on the data sent on the channel.