@ariakit/react-core
Version:
Ariakit React core
103 lines (101 loc) • 2.96 kB
JavaScript
"use client";
import {
useComboboxProviderContext
} from "../__chunks/7P7IWEBR.js";
import "../__chunks/MWF5B7KS.js";
import "../__chunks/63N3TCQX.js";
import "../__chunks/FFSBKSAM.js";
import "../__chunks/72IB6YNO.js";
import {
useButton
} from "../__chunks/J4DPQC5Y.js";
import "../__chunks/WJ7PHYH7.js";
import "../__chunks/US3TW2XI.js";
import "../__chunks/SWN3JYXT.js";
import "../__chunks/45YOMIF3.js";
import {
createElement,
createHook,
forwardRef
} from "../__chunks/ILRXHV7V.js";
import {
useEvent,
useWrapElement
} from "../__chunks/K2XTQB3X.js";
import "../__chunks/YXGXYGQX.js";
// src/combobox/combobox-cancel.tsx
import { invariant } from "@ariakit/core/utils/misc";
import { Fragment } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
var TagName = "button";
var children = /* @__PURE__ */ jsxs(
"svg",
{
"aria-hidden": "true",
display: "block",
viewBox: "0 0 16 16",
fill: "none",
stroke: "currentColor",
strokeLinecap: "round",
strokeLinejoin: "round",
strokeWidth: 1.5,
width: "1em",
height: "1em",
pointerEvents: "none",
children: [
/* @__PURE__ */ jsx("line", { x1: "5", y1: "5", x2: "11", y2: "11" }),
/* @__PURE__ */ jsx("line", { x1: "5", y1: "11", x2: "11", y2: "5" })
]
}
);
var useComboboxCancel = createHook(
function useComboboxCancel2({ store, hideWhenEmpty, ...props }) {
const context = useComboboxProviderContext();
store = store || context;
invariant(
store,
process.env.NODE_ENV !== "production" && "ComboboxCancel must receive a `store` prop or be wrapped in a ComboboxProvider component."
);
const onClickProp = props.onClick;
const onClick = useEvent((event) => {
onClickProp == null ? void 0 : onClickProp(event);
if (event.defaultPrevented) return;
store == null ? void 0 : store.setValue("");
store == null ? void 0 : store.move(null);
});
const comboboxId = store.useState((state) => {
var _a;
return (_a = state.baseElement) == null ? void 0 : _a.id;
});
const empty = store.useState((state) => state.value === "");
props = useWrapElement(
props,
(element) => {
if (!hideWhenEmpty) return element;
if (empty) return /* @__PURE__ */ jsx(Fragment, {});
return element;
},
[hideWhenEmpty, empty]
);
props = {
children,
"aria-label": "Clear input",
// This aria-controls will ensure the combobox popup remains visible when
// this element gets focused. This logic is done in the ComboboxPopover
// component.
"aria-controls": comboboxId,
...props,
onClick
};
props = useButton(props);
return props;
}
);
var ComboboxCancel = forwardRef(function ComboboxCancel2(props) {
const htmlProps = useComboboxCancel(props);
return createElement(TagName, htmlProps);
});
export {
ComboboxCancel,
useComboboxCancel
};