UNPKG

@wordpress/global-styles-ui

Version:
1 lines • 14 kB
{"version":3,"file":"screen-element.cjs","names":["unlock","blockEditorPrivateApis","__","useSetting","useCanEditCSS","normalizeBorderStyle","BACKGROUND_BLOCK_DEFAULT_VALUES","PanelBody","sprintf","useNavigator","useState","useMemo","getValidPseudoStates","useSelect","blocksStore","useStyle","ScreenHeader","ElementPreview","ElementBlockOverrides"],"sources":["../src/screen-element.tsx"],"sourcesContent":["import { __, sprintf } from '@wordpress/i18n';\nimport { useMemo, useState } from '@wordpress/element';\nimport { PanelBody, useNavigator } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { store as blocksStore } from '@wordpress/blocks';\nimport type { BlockType } from '@wordpress/blocks';\nimport { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';\nimport type {\n\tGlobalStylesSettings,\n\tGlobalStylesStyles,\n} from '@wordpress/global-styles-engine';\nimport { ScreenHeader } from './screen-header';\nimport ElementPreview from './element-preview';\nimport ElementBlockOverrides from './element-block-overrides';\nimport { useSetting, useStyle, useCanEditCSS } from './hooks';\nimport { unlock } from './lock-unlock';\nimport { getValidPseudoStates } from './utils';\n// Elements store borders and show background image defaults the same way as\n// blocks, so they share the block screen's handling.\nimport {\n\tBACKGROUND_BLOCK_DEFAULT_VALUES,\n\tnormalizeBorderStyle,\n} from './screen-block';\n\nconst {\n\tuseSettingsForBlockElement,\n\tuseHasBackgroundPanel,\n\tuseHasBorderPanel,\n\tuseHasDimensionsPanel,\n\tuseHasTypographyPanel,\n\tBackgroundPanel: StylesBackgroundPanel,\n\tBorderPanel: StylesBorderPanel,\n\tDimensionsPanel: StylesDimensionsPanel,\n\tTypographyPanel: StylesTypographyPanel,\n\tAdvancedPanel: StylesAdvancedPanel,\n\tgetBlockTypesForElement,\n} = unlock( blockEditorPrivateApis );\n\n/*\n * The elements that have their own screen. Their titles double as their labels\n * in the Blocks & Elements list, so the two always match.\n */\nexport const ELEMENTS = {\n\ttext: {\n\t\tdescription: __( 'Manage the fonts used on the site.' ),\n\t\ttitle: __( 'Text' ),\n\t},\n\tlink: {\n\t\tdescription: __( 'Manage the appearance of links.' ),\n\t\ttitle: __( 'Link' ),\n\t},\n\tcaption: {\n\t\tdescription: __( 'Manage the appearance of captions.' ),\n\t\ttitle: __( 'Caption' ),\n\t},\n\tcite: {\n\t\tdescription: __( 'Manage the appearance of citations.' ),\n\t\ttitle: __( 'Citation' ),\n\t},\n\theading: {\n\t\tdescription: __( 'Manage the appearance of all headings.' ),\n\t\ttitle: __( 'All headings' ),\n\t},\n\th1: {\n\t\tdescription: __( 'Manage the appearance of level 1 headings.' ),\n\t\ttitle: __( 'Heading 1' ),\n\t},\n\th2: {\n\t\tdescription: __( 'Manage the appearance of level 2 headings.' ),\n\t\ttitle: __( 'Heading 2' ),\n\t},\n\th3: {\n\t\tdescription: __( 'Manage the appearance of level 3 headings.' ),\n\t\ttitle: __( 'Heading 3' ),\n\t},\n\th4: {\n\t\tdescription: __( 'Manage the appearance of level 4 headings.' ),\n\t\ttitle: __( 'Heading 4' ),\n\t},\n\th5: {\n\t\tdescription: __( 'Manage the appearance of level 5 headings.' ),\n\t\ttitle: __( 'Heading 5' ),\n\t},\n\th6: {\n\t\tdescription: __( 'Manage the appearance of level 6 headings.' ),\n\t\ttitle: __( 'Heading 6' ),\n\t},\n\ttextInput: {\n\t\tdescription: __( 'Manage the appearance of inputs.' ),\n\t\ttitle: __( 'Form input' ),\n\t},\n\tselect: {\n\t\tdescription: __( 'Manage the appearance of selects.' ),\n\t\ttitle: __( 'Form select' ),\n\t},\n\tbutton: {\n\t\tdescription: __( 'Manage the appearance of buttons.' ),\n\t\ttitle: __( 'Form button' ),\n\t},\n};\n\nexport type ElementName = keyof typeof ELEMENTS;\n\ninterface ScreenElementProps {\n\t/**\n\t * The element to style. Read from the `:element` path parameter when not\n\t * given, which is how the elements in the block list open.\n\t */\n\telement?: ElementName;\n\t/**\n\t * Whether to offer the element's states, such as hover. Follows the same\n\t * editor setting that turns off state editing for blocks.\n\t */\n\tshowStateControls?: boolean;\n}\n\n/**\n * Renders every style panel for an element. The element's settings are\n * resolved once here and shared by all the panels, as are the styles that the\n * screen resolves for them.\n *\n * @param props\n * @param props.element The element being styled.\n * @param props.label Its display name, used in the custom CSS help text.\n * @param props.style The element's user styles in the selected state.\n * @param props.inheritedStyle The element's merged styles in the selected state.\n * @param props.setStyle Saves the element's user styles in the selected state.\n */\nfunction ElementStylePanels( {\n\telement,\n\tlabel,\n\tstyle,\n\tinheritedStyle,\n\tsetStyle,\n}: {\n\telement: string;\n\tlabel: string;\n\tstyle?: GlobalStylesStyles;\n\tinheritedStyle?: GlobalStylesStyles;\n\tsetStyle: ( newStyle?: GlobalStylesStyles ) => void;\n} ) {\n\t// The root has its own screens for background, spacing and borders, so\n\t// only typography is offered for \"Text\".\n\tconst isRoot = element === 'text';\n\tconst [ rawSettings ] = useSetting< GlobalStylesSettings >( '' );\n\tconst settings = useSettingsForBlockElement(\n\t\trawSettings,\n\t\tundefined,\n\t\telement\n\t);\n\n\tconst hasTypographyPanel = useHasTypographyPanel( settings );\n\tconst hasBackgroundPanel = useHasBackgroundPanel( settings ) && ! isRoot;\n\tconst hasDimensionsPanel = useHasDimensionsPanel( settings ) && ! isRoot;\n\tconst hasBorderPanel = useHasBorderPanel( settings ) && ! isRoot;\n\n\tconst canEditCSS = useCanEditCSS();\n\tconst onChangeBorders = ( newStyle: any ) =>\n\t\tsetStyle( normalizeBorderStyle( newStyle ) );\n\n\treturn (\n\t\t<>\n\t\t\t{ hasTypographyPanel && (\n\t\t\t\t<StylesTypographyPanel\n\t\t\t\t\tinheritedValue={ inheritedStyle }\n\t\t\t\t\tvalue={ style }\n\t\t\t\t\tonChange={ setStyle }\n\t\t\t\t\tsettings={ settings }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ hasBackgroundPanel && (\n\t\t\t\t<StylesBackgroundPanel\n\t\t\t\t\tinheritedValue={ inheritedStyle }\n\t\t\t\t\tvalue={ style }\n\t\t\t\t\tonChange={ setStyle }\n\t\t\t\t\tsettings={ settings }\n\t\t\t\t\tdefaultValues={ BACKGROUND_BLOCK_DEFAULT_VALUES }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ hasDimensionsPanel && (\n\t\t\t\t<StylesDimensionsPanel\n\t\t\t\t\tinheritedValue={ inheritedStyle }\n\t\t\t\t\tvalue={ style }\n\t\t\t\t\tonChange={ setStyle }\n\t\t\t\t\tsettings={ settings }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ hasBorderPanel && (\n\t\t\t\t<StylesBorderPanel\n\t\t\t\t\tinheritedValue={ inheritedStyle }\n\t\t\t\t\tvalue={ style }\n\t\t\t\t\tonChange={ onChangeBorders }\n\t\t\t\t\tsettings={ settings }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ canEditCSS && ! isRoot && (\n\t\t\t\t<PanelBody title={ __( 'Advanced' ) } initialOpen={ false }>\n\t\t\t\t\t<StylesAdvancedPanel\n\t\t\t\t\t\tvalue={ style }\n\t\t\t\t\t\tonChange={ setStyle }\n\t\t\t\t\t\tinheritedValue={ inheritedStyle }\n\t\t\t\t\t\thelp={ sprintf(\n\t\t\t\t\t\t\t// translators: %s: the name of an element e.g., 'Buttons' or 'Inputs'.\n\t\t\t\t\t\t\t__(\n\t\t\t\t\t\t\t\t'Add your own CSS to customize the appearance of %s. You do not need to include a CSS selector, just add the property and value.'\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\tlabel\n\t\t\t\t\t\t) }\n\t\t\t\t\t/>\n\t\t\t\t</PanelBody>\n\t\t\t) }\n\t\t</>\n\t);\n}\n\nfunction ScreenElement( {\n\telement: elementProp,\n\tshowStateControls = true,\n}: ScreenElementProps ) {\n\tconst { params } = useNavigator();\n\tconst element = ( elementProp ?? params.element ) as ElementName;\n\tconst [ selectedPseudoState, setSelectedPseudoState ] =\n\t\tuseState< string >( 'default' );\n\t// Only links and buttons have states, such as hover and focus.\n\tconst pseudoStates = useMemo(\n\t\t() => ( showStateControls ? getValidPseudoStates( element ) : [] ),\n\t\t[ element, showStateControls ]\n\t);\n\tconst allBlockTypes = useSelect(\n\t\t( select ) => select( blocksStore ).getBlockTypes(),\n\t\t[]\n\t);\n\tconst elementBlockTypes = useMemo(\n\t\t() =>\n\t\t\tgetBlockTypesForElement( element )\n\t\t\t\t.map( ( name: string ) =>\n\t\t\t\t\tallBlockTypes.find(\n\t\t\t\t\t\t( blockType ) => blockType.name === name\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t\t.filter(\n\t\t\t\t\t(\n\t\t\t\t\t\tblockType: BlockType | undefined\n\t\t\t\t\t): blockType is BlockType => !! blockType\n\t\t\t\t),\n\t\t[ element, allBlockTypes ]\n\t);\n\tconst state =\n\t\tselectedPseudoState === 'default' ? undefined : selectedPseudoState;\n\t// \"Text\" is the site's base text, stored at the root of the styles rather\n\t// than under `elements`. Its styles are resolved once here and shared by\n\t// the preview and the style panels.\n\tconst prefix = element === 'text' ? '' : `elements.${ element }`;\n\tconst [ style ] = useStyle< GlobalStylesStyles >(\n\t\tprefix,\n\t\t'',\n\t\t'user',\n\t\tfalse,\n\t\tstate\n\t);\n\tconst [ inheritedStyle, setStyle ] = useStyle< GlobalStylesStyles >(\n\t\tprefix,\n\t\t'',\n\t\t'merged',\n\t\tfalse,\n\t\tstate\n\t);\n\n\tif ( ! Object.hasOwn( ELEMENTS, element ) ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t<ScreenHeader\n\t\t\t\ttitle={ ELEMENTS[ element ].title }\n\t\t\t\tdescription={ ELEMENTS[ element ].description }\n\t\t\t\tpseudoStates={ pseudoStates }\n\t\t\t\tselectedPseudoState={ selectedPseudoState }\n\t\t\t\tonChangePseudoState={ setSelectedPseudoState }\n\t\t\t/>\n\t\t\t<ElementPreview\n\t\t\t\telement={ element }\n\t\t\t\tstate={ state }\n\t\t\t\tstateStyles={ state ? inheritedStyle : undefined }\n\t\t\t/>\n\t\t\t<ElementStylePanels\n\t\t\t\telement={ element }\n\t\t\t\tlabel={ ELEMENTS[ element ].title }\n\t\t\t\tstyle={ style }\n\t\t\t\tinheritedStyle={ inheritedStyle }\n\t\t\t\tsetStyle={ setStyle }\n\t\t\t/>\n\t\t\t<ElementBlockOverrides\n\t\t\t\telement={ element }\n\t\t\t\tblockTypes={ elementBlockTypes }\n\t\t\t/>\n\t\t</>\n\t);\n}\n\nexport default ScreenElement;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAM,EACL,4BACA,uBACA,mBACA,uBACA,uBACA,iBAAiB,uBACjB,aAAa,mBACb,iBAAiB,uBACjB,iBAAiB,uBACjB,eAAe,qBACf,6BAAA,GACGA,iBAAAA,OAAAA,CAAQC,wBAAAA,WAAuB;AAMnC,MAAa,WAAW;CACvB,MAAM;EACL,cAAA,GAAaC,gBAAAA,GAAAA,CAAI,oCAAqC;EACtD,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,MAAO;CACnB;CACA,MAAM;EACL,cAAA,GAAaA,gBAAAA,GAAAA,CAAI,iCAAkC;EACnD,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,MAAO;CACnB;CACA,SAAS;EACR,cAAA,GAAaA,gBAAAA,GAAAA,CAAI,oCAAqC;EACtD,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,SAAU;CACtB;CACA,MAAM;EACL,cAAA,GAAaA,gBAAAA,GAAAA,CAAI,qCAAsC;EACvD,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,UAAW;CACvB;CACA,SAAS;EACR,cAAA,GAAaA,gBAAAA,GAAAA,CAAI,wCAAyC;EAC1D,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,cAAe;CAC3B;CACA,IAAI;EACH,cAAA,GAAaA,gBAAAA,GAAAA,CAAI,4CAA6C;EAC9D,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,WAAY;CACxB;CACA,IAAI;EACH,cAAA,GAAaA,gBAAAA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