@wordpress/components
Version:
UI components for WordPress.
71 lines (70 loc) • 2.54 kB
JavaScript
import clsx from "clsx";
import { useInstanceId } from "@wordpress/compose";
import { forwardRef, useContext, useEffect } from "@wordpress/element";
import { Icon, check } from "@wordpress/icons";
import { CircularOptionPickerContext } from "./circular-option-picker-context.mjs";
import Button from "../button/index.mjs";
import { Composite } from "../composite/index.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/components/src/circular-option-picker/circular-option-picker-option.tsx
function UnforwardedOptionAsButton(props, forwardedRef) {
const { isPressed, label, ...additionalProps } = props;
return /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
...additionalProps,
"aria-pressed": isPressed,
ref: forwardedRef,
label
});
}
const OptionAsButton = forwardRef(UnforwardedOptionAsButton);
function UnforwardedOptionAsOption(props, forwardedRef) {
const { id, isSelected, label, ...additionalProps } = props;
const { setActiveId, activeId } = useContext(CircularOptionPickerContext);
useEffect(() => {
if (isSelected && !activeId) window.setTimeout(() => setActiveId?.(id), 0);
}, [
isSelected,
setActiveId,
activeId,
id
]);
return /* @__PURE__ */ jsx(Composite.Item, {
render: /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
...additionalProps,
role: "option",
"aria-selected": !!isSelected,
ref: forwardedRef,
label
}),
id
});
}
const OptionAsOption = forwardRef(UnforwardedOptionAsOption);
function Option({ className, isSelected, selectedIconProps = {}, tooltipText, ...additionalProps }) {
const { baseId, presentation = "toggle-buttons" } = useContext(CircularOptionPickerContext);
const commonProps = {
id: useInstanceId(Option, baseId || "components-circular-option-picker__option"),
className: "components-circular-option-picker__option",
...additionalProps
};
const optionControl = presentation === "listbox" ? /* @__PURE__ */ jsx(OptionAsOption, {
...commonProps,
label: tooltipText,
isSelected
}) : /* @__PURE__ */ jsx(OptionAsButton, {
...commonProps,
label: tooltipText,
isPressed: presentation === "toggle-buttons" ? !!isSelected : void 0
});
return /* @__PURE__ */ jsxs("div", {
className: clsx(className, "components-circular-option-picker__option-wrapper"),
children: [optionControl, presentation !== "command-buttons" && isSelected && /* @__PURE__ */ jsx(Icon, {
icon: check,
...selectedIconProps
})]
});
}
export { Option };
//# sourceMappingURL=circular-option-picker-option.mjs.map