UNPKG

@wordpress/block-library

Version:
251 lines (250 loc) 8.14 kB
// packages/block-library/src/social-links/edit.js import clsx from "clsx"; import { useEffect } from "@wordpress/element"; import { useInnerBlocksProps, useBlockProps, InspectorControls, ContrastChecker, withColors, InnerBlocks, __experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown, __experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients, store as blockEditorStore } from "@wordpress/block-editor"; import { ToggleControl, SelectControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { useSelect } from "@wordpress/data"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; var sizeOptions = [ { label: __("Default"), value: "" }, { label: __("Small"), value: "has-small-icon-size" }, { label: __("Normal"), value: "has-normal-icon-size" }, { label: __("Large"), value: "has-large-icon-size" }, { label: __("Huge"), value: "has-huge-icon-size" } ]; function SocialLinksEdit(props) { const { clientId, attributes, iconBackgroundColor, iconColor, isSelected, setAttributes, setIconBackgroundColor, setIconColor } = props; const { iconBackgroundColorValue, iconColorValue, openInNewTab, showLabels, size } = attributes; const { hasSocialIcons, hasSelectedChild } = useSelect( (select) => { const { getBlockCount, hasSelectedInnerBlock } = select(blockEditorStore); return { hasSocialIcons: getBlockCount(clientId) > 0, hasSelectedChild: hasSelectedInnerBlock(clientId) }; }, [clientId] ); const hasAnySelected = isSelected || hasSelectedChild; const logosOnly = attributes.className?.includes("is-style-logos-only"); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); useEffect(() => { if (logosOnly) { let restore; setAttributes((prev) => { restore = { iconBackgroundColor: prev.iconBackgroundColor, iconBackgroundColorValue: prev.iconBackgroundColorValue, customIconBackgroundColor: prev.customIconBackgroundColor }; return { iconBackgroundColor: void 0, iconBackgroundColorValue: void 0, customIconBackgroundColor: void 0 }; }); return () => setAttributes({ ...restore }); } }, [logosOnly, setAttributes]); const className = clsx(size, { "has-visible-labels": showLabels, "has-icon-color": iconColor.color || iconColorValue, "has-icon-background-color": iconBackgroundColor.color || iconBackgroundColorValue }); const blockProps = useBlockProps({ className }); const innerBlocksProps = useInnerBlocksProps(blockProps, { templateLock: false, orientation: attributes.layout?.orientation ?? "horizontal", __experimentalAppenderTagName: "li", renderAppender: !hasSocialIcons || hasAnySelected ? InnerBlocks.ButtonBlockAppender : void 0 }); const colorSettings = [ { // Use custom attribute as fallback to prevent loss of named color selection when // switching themes to a new theme that does not have a matching named color. value: iconColor.color || iconColorValue, onChange: (colorValue) => { setIconColor(colorValue); setAttributes({ iconColorValue: colorValue }); }, label: __("Icon color"), resetAllFilter: () => { setIconColor(void 0); setAttributes({ iconColorValue: void 0 }); } } ]; if (!logosOnly) { colorSettings.push({ // Use custom attribute as fallback to prevent loss of named color selection when // switching themes to a new theme that does not have a matching named color. value: iconBackgroundColor.color || iconBackgroundColorValue, onChange: (colorValue) => { setIconBackgroundColor(colorValue); setAttributes({ iconBackgroundColorValue: colorValue }); }, label: __("Icon background"), resetAllFilter: () => { setIconBackgroundColor(void 0); setAttributes({ iconBackgroundColorValue: void 0 }); } }); } const colorGradientSettings = useMultipleOriginColorsAndGradients(); return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs( ToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ openInNewTab: false, showLabels: false, size: void 0 }); }, dropdownMenuProps, children: [ /* @__PURE__ */ jsx( ToolsPanelItem, { isShownByDefault: true, hasValue: () => !!size, label: __("Icon size"), onDeselect: () => setAttributes({ size: void 0 }), children: /* @__PURE__ */ jsx( SelectControl, { __next40pxDefaultSize: true, label: __("Icon size"), onChange: (newSize) => { setAttributes({ size: newSize === "" ? void 0 : newSize }); }, value: size ?? "", options: sizeOptions } ) } ), /* @__PURE__ */ jsx( ToolsPanelItem, { isShownByDefault: true, label: __("Show text"), hasValue: () => !!showLabels, onDeselect: () => setAttributes({ showLabels: false }), children: /* @__PURE__ */ jsx( ToggleControl, { label: __("Show text"), checked: showLabels, onChange: () => setAttributes({ showLabels: !showLabels }) } ) } ), /* @__PURE__ */ jsx( ToolsPanelItem, { isShownByDefault: true, label: __("Open links in new tab"), hasValue: () => !!openInNewTab, onDeselect: () => setAttributes({ openInNewTab: false }), children: /* @__PURE__ */ jsx( ToggleControl, { label: __("Open links in new tab"), checked: openInNewTab, onChange: () => setAttributes({ openInNewTab: !openInNewTab }) } ) } ) ] } ) }), colorGradientSettings.hasColorsOrGradients && /* @__PURE__ */ jsxs(InspectorControls, { group: "color", children: [ colorSettings.map( ({ onChange, label, value, resetAllFilter }) => /* @__PURE__ */ jsx( ColorGradientSettingsDropdown, { __experimentalIsRenderedInSidebar: true, settings: [ { colorValue: value, label, onColorChange: onChange, isShownByDefault: true, resetAllFilter, enableAlpha: true, clearable: true } ], panelId: clientId, ...colorGradientSettings }, `social-links-color-${label}` ) ), !logosOnly && /* @__PURE__ */ jsx( ContrastChecker, { ...{ textColor: iconColorValue, backgroundColor: iconBackgroundColorValue }, isLargeText: false } ) ] }), /* @__PURE__ */ jsx("ul", { ...innerBlocksProps }) ] }); } var iconColorAttributes = { iconColor: "icon-color", iconBackgroundColor: "icon-background-color" }; var edit_default = withColors(iconColorAttributes)(SocialLinksEdit); export { SocialLinksEdit, edit_default as default }; //# sourceMappingURL=edit.mjs.map