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

Storybook: Add story for TypographyPanel #35293

Merged
merged 4 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/components/src/tools-panel/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ export const WithSlotFillItems = () => {
);
};

export { TypographyPanel } from './typography-panel';

const PanelWrapperView = styled.div`
max-width: 260px;
font-size: 13px;
Expand Down
215 changes: 215 additions & 0 deletions packages/components/src/tools-panel/stories/typography-panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/**
* WordPress dependencies
*/
import {
__experimentalFontAppearanceControl as FontAppearanceControl,
__experimentalFontFamilyControl as FontFamilyControl,
__experimentalLetterSpacingControl as LetterSpacingControl,
LineHeightControl,
} from '@wordpress/block-editor';
Comment on lines +4 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reasoning behind picking these controls from @wordpress/block-editor, but picking FontSizePicker from @wordpress/components ?

In general, for the purpose of this Storybook example, should we use components from @wordpress/block-editor or from @wordpress/components?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just imported the components used in the existing TypographyPanel for the most part.

I don't think it really matters which things we use here (existing block-editor components or wp-component primitives) at least in this PR, as they are only placeholders right now. We'll be evaluating each piece on a case-by-case basis, and replace when necessary. As a starting point, I'm inclined toward having the "real world" block-editor components as a reference so we don't make assumptions based on static mockups only. (e.g. This has already surfaced a concern with the Letter Spacing mockup, which doesn't take units into account.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me. I was just curious about why we didn't also pick FontSizePicker from @wordpress/block-editor.

As a starting point, I'm inclined toward having the "real world" block-editor components as a reference so we don't make assumptions based on static mockups only.

Makes sense, let's go with your approach!

import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import FontSizePicker from '../../font-size-picker';
import Panel from '../../panel';
import {
ToggleGroupControl,
ToggleGroupControlOption,
} from '../../toggle-group-control';
import { ToolsPanel, ToolsPanelItem } from '..';

// These options match the theme.json typography schema
const fontFamilies = [
{
fontFamily:
'-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif',
slug: 'system-fonts',
name: 'System Fonts',
},
{
fontFamily:
'Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace',
name: 'Monospace',
slug: 'monospace',
},
{
fontFamily:
'Hoefler Text, Baskerville Old Face, Garamond, Times New Roman, serif',
name: 'Serif',
slug: 'hoefler-times-new-roman',
},
];
const fontSizes = [
{
slug: 'small',
size: '1rem',
name: 'Small',
},
{
slug: 'normal',
size: '1.25rem',
name: 'Normal',
},
{
slug: 'medium',
size: '1.5rem',
name: 'medium',
},
{
slug: 'large',
size: '2rem',
name: 'Large',
},
{
slug: 'extra-large',
size: '3rem',
name: 'Extra large',
},
];

const LetterCaseControl = ( props ) => {
mirka marked this conversation as resolved.
Show resolved Hide resolved
return (
<ToggleGroupControl label="Letter Case" isBlock={ true } { ...props }>
{ [
{ value: 'uppercase', ariaLabel: 'Uppercase', label: 'AB' },
{ value: 'lowercase', ariaLabel: 'Lowercase', label: 'ab' },
{ value: 'capitalize', ariaLabel: 'Capitalize', label: 'Ab' },
].map( ( { value, ariaLabel, label } ) => (
<ToggleGroupControlOption
key={ value }
label={ label }
aria-label={ ariaLabel }
value={ value }
/>
) ) }
</ToggleGroupControl>
);
};

export const TypographyPanel = () => {
const [ fontFamily, setFontFamily ] = useState();
const [ fontSize, setFontSize ] = useState();
const [ fontWeight, setFontWeight ] = useState();
const [ fontStyle, setFontStyle ] = useState();
const [ lineHeight, setLineHeight ] = useState();
const [ letterSpacing, setLetterSpacing ] = useState();
const [ textTransform, setTextTransform ] = useState();

return (
<>
<div style={ { maxWidth: '280px' } }>
<Panel>
<ToolsPanel label="Typography">
<ToolsPanelItem
hasValue={ () => !! fontFamily }
label="Font"
onDeselect={ () => setFontFamily( undefined ) }
isShownByDefault={ true }
>
<FontFamilyControl
value={ fontFamily }
onChange={ setFontFamily }
fontFamilies={ fontFamilies }
/>
</ToolsPanelItem>

<ToolsPanelItem
hasValue={ () => !! fontSize }
label="Size"
onDeselect={ () => setFontSize( undefined ) }
isShownByDefault={ true }
>
<FontSizePicker
value={ fontSize }
onChange={ setFontSize }
fontSizes={ fontSizes }
/>
</ToolsPanelItem>

<ToolsPanelItem
hasValue={ () => !! fontStyle || !! fontWeight }
label="Appearance"
onDeselect={ () => {
setFontStyle( undefined );
setFontWeight( undefined );
} }
isShownByDefault={ true }
>
<FontAppearanceControl
value={ {
fontStyle,
fontWeight,
} }
onChange={ ( {
fontStyle: style,
fontWeight: weight,
} ) => {
setFontStyle( style );
setFontWeight( weight );
} }
/>
</ToolsPanelItem>

<ToolsPanelItem
hasValue={ () => !! lineHeight }
label="Line Height"
onDeselect={ () => setLineHeight( undefined ) }
isShownByDefault={ true }
>
<LineHeightControl
value={ lineHeight }
onChange={ setLineHeight }
/>
</ToolsPanelItem>

<ToolsPanelItem
hasValue={ () => !! letterSpacing }
label="Letter Spacing"
onDeselect={ () => setLetterSpacing( undefined ) }
isShownByDefault={ true }
>
<LetterSpacingControl
value={ letterSpacing }
onChange={ setLetterSpacing }
/>
</ToolsPanelItem>

<ToolsPanelItem
hasValue={ () => !! textTransform }
label="Letter Case"
onDeselect={ () => setTextTransform( undefined ) }
isShownByDefault={ true }
>
<LetterCaseControl
onChange={ setTextTransform }
value={ textTransform }
/>
</ToolsPanelItem>
</ToolsPanel>
</Panel>
</div>
<div
style={ {
marginTop: '1rem',
fontFamily,
fontSize,
fontStyle,
fontWeight,
lineHeight,
letterSpacing,
textTransform,
} }
>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</>
);
};