-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5535 from gaetanmaisse/ts-migration/addon-backgro…
…unds Migrate addon backgrounds to TS
- Loading branch information
Showing
16 changed files
with
222 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { styled } from '@storybook/theming'; | ||
|
||
export const ColorIcon = styled.span(({ background }: { background: string }) => ({ | ||
borderRadius: '1rem', | ||
display: 'block', | ||
height: '1rem', | ||
width: '1rem', | ||
background, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ColorIcon'; |
2 changes: 1 addition & 1 deletion
2
addons/backgrounds/src/constants.js → addons/backgrounds/src/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export const ADDON_ID = 'storybook/background'; | ||
export const PARAM_KEY = 'backgrounds'; | ||
|
||
export default { | ||
export const EVENTS = { | ||
SET: `${ADDON_ID}:set`, | ||
UNSET: `${ADDON_ID}:unset`, | ||
}; |
165 changes: 165 additions & 0 deletions
165
addons/backgrounds/src/containers/BackgroundSelector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import React, { Component, Fragment } from 'react'; | ||
import memoize from 'memoizerific'; | ||
|
||
import { Global } from '@storybook/theming'; | ||
|
||
import { SET_STORIES } from '@storybook/core-events'; | ||
|
||
import { Icons, IconButton, WithTooltip, TooltipLinkList } from '@storybook/components'; | ||
|
||
import { PARAM_KEY } from '../constants'; | ||
import { ColorIcon } from '../components'; | ||
import { BackgroundConfig, BackgroundSelectorItem } from '../models'; | ||
|
||
const iframeId = 'storybook-preview-background'; | ||
|
||
const createBackgroundSelectorItem = memoize(1000)( | ||
( | ||
id: string, | ||
name: string, | ||
value: string, | ||
hasSwatch: boolean, | ||
change: (arg: { selected: string; expanded: boolean }) => void | ||
): BackgroundSelectorItem => ({ | ||
id: id || name, | ||
title: name, | ||
onClick: () => { | ||
change({ selected: value, expanded: false }); | ||
}, | ||
value, | ||
right: hasSwatch ? <ColorIcon background={value} /> : undefined, | ||
}) | ||
); | ||
|
||
const getSelectedBackgroundColor = ( | ||
list: BackgroundConfig[], | ||
currentSelectedValue: string | ||
): string => { | ||
if (!list.length) { | ||
return 'transparent'; | ||
} | ||
|
||
if (currentSelectedValue === 'transparent') { | ||
return currentSelectedValue; | ||
} | ||
|
||
if (list.find(i => i.value === currentSelectedValue)) { | ||
return currentSelectedValue; | ||
} | ||
|
||
if (list.find(i => i.default)) { | ||
return list.find(i => i.default).value; | ||
} | ||
|
||
return 'transparent'; | ||
}; | ||
|
||
const getDisplayableState = memoize(10)( | ||
(props: BackgroundToolProps, state: BackgroundToolState, change) => { | ||
const data = props.api.getCurrentStoryData(); | ||
const list: BackgroundConfig[] = (data && data.parameters && data.parameters[PARAM_KEY]) || []; | ||
|
||
const selectedBackgroundColor = getSelectedBackgroundColor(list, state.selected); | ||
|
||
let availableBackgroundSelectorItems: BackgroundSelectorItem[] = []; | ||
|
||
if (selectedBackgroundColor !== 'transparent') { | ||
availableBackgroundSelectorItems.push( | ||
createBackgroundSelectorItem('reset', 'Clear background', 'transparent', false, change) | ||
); | ||
} | ||
|
||
if (list.length) { | ||
availableBackgroundSelectorItems = [ | ||
...availableBackgroundSelectorItems, | ||
...list.map(({ name, value }) => | ||
createBackgroundSelectorItem(null, name, value, true, change) | ||
), | ||
]; | ||
} | ||
|
||
return { | ||
items: availableBackgroundSelectorItems, | ||
selectedBackgroundColor, | ||
}; | ||
} | ||
); | ||
|
||
interface BackgroundToolProps { | ||
api: { | ||
on(event: string, callback: (data: any) => void): void; | ||
off(event: string, callback: (data: any) => void): void; | ||
getCurrentStoryData(): any; | ||
}; | ||
} | ||
|
||
interface BackgroundToolState { | ||
items: BackgroundSelectorItem[]; | ||
selected: string; | ||
expanded: boolean; | ||
} | ||
|
||
export class BackgroundSelector extends Component<BackgroundToolProps, BackgroundToolState> { | ||
private listener = () => { | ||
this.setState({ selected: null }); | ||
}; | ||
|
||
constructor(props: BackgroundToolProps) { | ||
super(props); | ||
|
||
this.state = { | ||
items: [], | ||
selected: null, | ||
expanded: false, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
const { api } = this.props; | ||
api.on(SET_STORIES, this.listener); | ||
} | ||
|
||
componentWillUnmount() { | ||
const { api } = this.props; | ||
api.off(SET_STORIES, this.listener); | ||
} | ||
|
||
change = (args: { selected: string; expanded: boolean }) => this.setState(args); | ||
|
||
render() { | ||
const { expanded } = this.state; | ||
const { items, selectedBackgroundColor } = getDisplayableState( | ||
this.props, | ||
this.state, | ||
this.change | ||
); | ||
|
||
return items.length ? ( | ||
<Fragment> | ||
{selectedBackgroundColor ? ( | ||
<Global | ||
styles={{ | ||
[`#${iframeId}`]: { | ||
background: selectedBackgroundColor, | ||
}, | ||
}} | ||
/> | ||
) : null} | ||
<WithTooltip | ||
placement="top" | ||
trigger="click" | ||
tooltipShown={expanded} | ||
onVisibilityChange={(newVisibility: boolean) => | ||
this.setState({ expanded: newVisibility }) | ||
} | ||
tooltip={<TooltipLinkList links={items} />} | ||
closeOnClick | ||
> | ||
<IconButton key="background" title="Backgrounds"> | ||
<Icons icon="photo" /> | ||
</IconButton> | ||
</WithTooltip> | ||
</Fragment> | ||
) : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './BackgroundSelector'; |
File renamed without changes.
Oops, something went wrong.