UNPKG

@wordpress/block-library

Version:
181 lines (180 loc) • 6.51 kB
import clsx from "clsx"; import { useEffect } from "@wordpress/element"; import { ContrastChecker, InnerBlocks, InspectorControls, __experimentalColorGradientSettingsDropdown, __experimentalUseMultipleOriginColorsAndGradients, store, useBlockProps, useInnerBlocksProps, withColors } from "@wordpress/block-editor"; import { ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { useSelect } from "@wordpress/data"; import { SelectControl } from "@wordpress/ui"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/social-links/edit.jsx const sizeItems = [ { label: __("Default"), value: null }, { 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, hasSelectedStyleState } = useSelect((select) => { const { getBlockCount, hasSelectedInnerBlock, hasSelectedBlockStyleState } = select(store); return { hasSocialIcons: getBlockCount(clientId) > 0, hasSelectedChild: hasSelectedInnerBlock(clientId), hasSelectedStyleState: hasSelectedBlockStyleState(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 = [{ 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({ value: iconBackgroundColor.color || iconBackgroundColorValue, onChange: (colorValue) => { setIconBackgroundColor(colorValue); setAttributes({ iconBackgroundColorValue: colorValue }); }, label: __("Icon background"), resetAllFilter: () => { setIconBackgroundColor(void 0); setAttributes({ iconBackgroundColorValue: void 0 }); } }); const colorGradientSettings = __experimentalUseMultipleOriginColorsAndGradients(); const showColorControls = colorGradientSettings.hasColorsOrGradients && !hasSelectedStyleState; return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ openInNewTab: false, showLabels: false, size: void 0 }); }, dropdownMenuProps, children: [ /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { isShownByDefault: true, hasValue: () => !!size, label: __("Icon size"), onDeselect: () => setAttributes({ size: void 0 }), children: /* @__PURE__ */ jsx(SelectControl, { label: __("Icon size"), onValueChange: (item) => item && setAttributes({ size: item.value ?? void 0 }), value: sizeItems.find((item) => item.value === (size || null)) ?? sizeItems[0], items: sizeItems }) }), /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { 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(__experimentalToolsPanelItem, { 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 }) }) }) ] }) }), showColorControls && /* @__PURE__ */ jsxs(InspectorControls, { group: "color", children: [colorSettings.map(({ onChange, label, value, resetAllFilter }) => /* @__PURE__ */ jsx(__experimentalColorGradientSettingsDropdown, { __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 edit_default = withColors({ iconColor: "icon-color", iconBackgroundColor: "icon-background-color" })(SocialLinksEdit); export { SocialLinksEdit, edit_default as default }; //# sourceMappingURL=edit.mjs.map