@wordpress/block-library
Version:
Block library for the WordPress editor.
276 lines (275 loc) • 8.75 kB
JavaScript
// packages/block-library/src/social-link/edit.js
import clsx from "clsx";
import { DELETE, BACKSPACE, ENTER } from "@wordpress/keycodes";
import { useDispatch, useSelect } from "@wordpress/data";
import {
BlockControls,
InspectorControls,
URLPopover,
URLInput,
useBlockEditingMode,
useBlockProps,
store as blockEditorStore
} from "@wordpress/block-editor";
import { useState, useRef, createInterpolateElement } from "@wordpress/element";
import {
Icon,
Button,
Dropdown,
TextControl,
ToolbarButton,
ExternalLink,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalInputControlSuffixWrapper as InputControlSuffixWrapper
} from "@wordpress/components";
import { useMergeRefs } from "@wordpress/compose";
import { __ } from "@wordpress/i18n";
import { keyboardReturn } from "@wordpress/icons";
import { store as blocksStore } from "@wordpress/blocks";
import { getSocialService } from "./social-list";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var SocialLinkURLPopover = ({
url,
setAttributes,
setPopover,
popoverAnchor,
clientId
}) => {
const { removeBlock } = useDispatch(blockEditorStore);
return /* @__PURE__ */ jsx(
URLPopover,
{
anchor: popoverAnchor,
"aria-label": __("Edit social link"),
onClose: () => {
setPopover(false);
popoverAnchor?.focus();
},
children: /* @__PURE__ */ jsx(
"form",
{
className: "block-editor-url-popover__link-editor",
onSubmit: (event) => {
event.preventDefault();
setPopover(false);
popoverAnchor?.focus();
},
children: /* @__PURE__ */ jsx("div", { className: "block-editor-url-input", children: /* @__PURE__ */ jsx(
URLInput,
{
value: url,
onChange: (nextURL) => setAttributes({ url: nextURL }),
placeholder: __("Enter social link"),
label: __("Enter social link"),
hideLabelFromVision: true,
disableSuggestions: true,
onKeyDown: (event) => {
if (!!url || event.defaultPrevented || ![BACKSPACE, DELETE].includes(
event.keyCode
)) {
return;
}
removeBlock(clientId);
},
suffix: /* @__PURE__ */ jsx(InputControlSuffixWrapper, { variant: "control", children: /* @__PURE__ */ jsx(
Button,
{
icon: keyboardReturn,
label: __("Apply"),
type: "submit",
size: "small"
}
) })
}
) })
}
)
}
);
};
var SocialLinkEdit = ({
attributes,
context,
isSelected,
setAttributes,
clientId,
name
}) => {
const { url, service, label = "", rel } = attributes;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const {
showLabels,
iconColor,
iconColorValue,
iconBackgroundColor,
iconBackgroundColorValue
} = context;
const [showURLPopover, setPopover] = useState(false);
const wrapperClasses = clsx(
"wp-social-link",
// Manually adding this class for backwards compatibility of CSS when moving the
// blockProps from the li to the button: https://github.com/WordPress/gutenberg/pull/64883
"wp-block-social-link",
"wp-social-link-" + service,
{
"wp-social-link__is-incomplete": !url,
[`has-${iconColor}-color`]: iconColor,
[`has-${iconBackgroundColor}-background-color`]: iconBackgroundColor
}
);
const [popoverAnchor, setPopoverAnchor] = useState(null);
const isContentOnlyMode = useBlockEditingMode() === "contentOnly";
const { activeVariation } = useSelect(
(select) => {
const { getActiveBlockVariation } = select(blocksStore);
return {
activeVariation: getActiveBlockVariation(name, attributes)
};
},
[name, attributes]
);
const { icon, label: socialLinkName } = getSocialService(activeVariation);
const socialLinkText = label.trim() === "" ? socialLinkName : label;
const ref = useRef();
const blockProps = useBlockProps({
className: "wp-block-social-link-anchor",
ref: useMergeRefs([setPopoverAnchor, ref]),
onClick: () => setPopover(true),
onKeyDown: (event) => {
if (event.keyCode === ENTER) {
event.preventDefault();
setPopover(true);
}
}
});
return /* @__PURE__ */ jsxs(Fragment, { children: [
isContentOnlyMode && showLabels && // Add an extra control to modify the label attribute when content only mode is active.
// With content only mode active, the inspector is hidden, so users need another way
// to edit this attribute.
/* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx(
Dropdown,
{
popoverProps: { placement: "bottom-start" },
renderToggle: ({ isOpen, onToggle }) => /* @__PURE__ */ jsx(
ToolbarButton,
{
onClick: onToggle,
"aria-haspopup": "true",
"aria-expanded": isOpen,
children: __("Text")
}
),
renderContent: () => /* @__PURE__ */ jsx(
TextControl,
{
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
className: "wp-block-social-link__toolbar_content_text",
label: __("Text"),
help: __(
"Provide a text label or use the default."
),
value: label,
onChange: (value) => setAttributes({ label: value }),
placeholder: socialLinkName
}
)
}
) }),
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(
ToolsPanel,
{
label: __("Settings"),
resetAll: () => {
setAttributes({ label: void 0 });
},
dropdownMenuProps,
children: /* @__PURE__ */ jsx(
ToolsPanelItem,
{
isShownByDefault: true,
label: __("Text"),
hasValue: () => !!label,
onDeselect: () => {
setAttributes({ label: void 0 });
},
children: /* @__PURE__ */ jsx(
TextControl,
{
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
label: __("Text"),
help: __(
"The text is visible when enabled from the parent Social Icons block."
),
value: label,
onChange: (value) => setAttributes({ label: value }),
placeholder: socialLinkName
}
)
}
)
}
) }),
/* @__PURE__ */ jsx(InspectorControls, { group: "advanced", children: /* @__PURE__ */ jsx(
TextControl,
{
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
label: __("Link relation"),
help: createInterpolateElement(
__(
"The <a>Link Relation</a> attribute defines the relationship between a linked resource and the current document."
),
{
a: /* @__PURE__ */ jsx(ExternalLink, { href: "https://developer.mozilla.org/docs/Web/HTML/Attributes/rel" })
}
),
value: rel || "",
onChange: (value) => setAttributes({ rel: value })
}
) }),
/* @__PURE__ */ jsxs(
"li",
{
role: "presentation",
className: wrapperClasses,
style: {
color: iconColorValue,
backgroundColor: iconBackgroundColorValue
},
children: [
/* @__PURE__ */ jsxs("button", { "aria-haspopup": "dialog", ...blockProps, role: "button", children: [
/* @__PURE__ */ jsx(Icon, { icon }),
/* @__PURE__ */ jsx(
"span",
{
className: clsx("wp-block-social-link-label", {
"screen-reader-text": !showLabels
}),
children: socialLinkText
}
)
] }),
isSelected && showURLPopover && /* @__PURE__ */ jsx(
SocialLinkURLPopover,
{
url,
setAttributes,
setPopover,
popoverAnchor,
clientId
}
)
]
}
)
] });
};
var edit_default = SocialLinkEdit;
export {
edit_default as default
};
//# sourceMappingURL=edit.js.map