UNPKG

@crossed/demo

Version:

A universal & performant styling library for React Native, Next.js & React

135 lines (134 loc) 4.51 kB
import { jsx, jsxs } from "react/jsx-runtime"; import { merge } from "@crossed/styled"; import { createSelect, useSelectContext } from "@crossed/primitive"; import { UilAngleDown, YBox } from "@crossed/ui"; import { forwardRef } from "react"; import { PortalProvider, Portal } from "@gorhom/portal"; import { composeRefs, createScope } from "@crossed/core"; import { autoUpdate, flip, offset, shift, useClick, useDismiss, useFloating, useInteractions, useRole } from "@floating-ui/react"; const [FloatingProvider, useFloatingProvider] = createScope( {} ); const Select = createSelect({ Root: (props) => { const { open, setOpen } = useSelectContext(); const floating = useFloating({ open, onOpenChange: setOpen, middleware: [offset(10), flip(), shift()], whileElementsMounted: autoUpdate }); const { context } = floating; const click = useClick(context); const dismiss = useDismiss(context); const role = useRole(context); const interactions = useInteractions([click, dismiss, role]); return /* @__PURE__ */ jsx(FloatingProvider, { ...floating, ...interactions, children: /* @__PURE__ */ jsx(PortalProvider, { children: /* @__PURE__ */ jsx("div", { ...props, className: merge(props.className, "flex flex-col") }) }) }); }, Trigger: forwardRef((props, ref) => { const { getReferenceProps, refs } = useFloatingProvider(); return /* @__PURE__ */ jsx( "button", { ...getReferenceProps(), ...props, ref: composeRefs(refs.setReference, ref), className: merge( "flex flex-col p-2 border border-neutral-800 rounded-md", props.className ) } ); }), Portal: ({ children }) => { const context = useFloatingProvider(); return /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx(FloatingProvider, { ...context, children }) }); }, Content: (props) => { const { refs, floatingStyles, getFloatingProps } = useFloatingProvider(); return /* @__PURE__ */ jsx( "div", { ref: refs.setFloating, ...getFloatingProps(), ...props, style: { ...props.style, ...floatingStyles, zIndex: 30, minWidth: 200 }, className: merge( "flex flex-col p-1 border bg-neutral-900 border-neutral-800 rounded-md w-auto", props.className ) } ); }, Item: forwardRef( (props, ref) => { return /* @__PURE__ */ jsx( "button", { ...props, ref, className: merge( "text-left rounded px-2 py-1 hover:bg-neutral-800", props.disabled && "opacity-50", props.className ) } ); } ), Label: forwardRef((props, ref) => { return /* @__PURE__ */ jsx( "div", { ...props, ref, className: "text-neutral-500 px-2 py-1 font-semibold" } ); }), Divider: forwardRef((props, ref) => { return /* @__PURE__ */ jsx("div", { ...props, ref, className: "border-t border-neutral-800 my-1" }); }) }); const CreateSelectSimpleDemo = () => { return /* @__PURE__ */ jsx(YBox, { space: "md", children: /* @__PURE__ */ jsxs(Select, { children: [ /* @__PURE__ */ jsxs( Select.Trigger, { "aria-label": "Click for toggle nav", className: "flex flex-row gap-2 items-center", children: [ "Hello", /* @__PURE__ */ jsx(UilAngleDown, {}) ] } ), /* @__PURE__ */ jsx(Select.Portal, { children: /* @__PURE__ */ jsxs(Select.Content, { children: [ /* @__PURE__ */ jsx(Select.Label, { children: "Application" }), /* @__PURE__ */ jsx(Select.Item, { "aria-label": "Settings", children: "Settings" }), /* @__PURE__ */ jsx(Select.Item, { "aria-label": "Settings", children: "Messages" }), /* @__PURE__ */ jsx(Select.Item, { "aria-label": "Settings", children: "Gallery" }), /* @__PURE__ */ jsx(Select.Divider, {}), /* @__PURE__ */ jsx(Select.Label, { children: "Danger zone" }), /* @__PURE__ */ jsx(Select.Item, { "aria-label": "Transfer my data", children: "Transfer my data" }), /* @__PURE__ */ jsx(Select.Item, { "aria-label": "Delete my account", children: "Delete my account" }), /* @__PURE__ */ jsx(Select.Item, { disabled: true, "aria-label": "Settings", children: "Users (not right)" }) ] }) }) ] }) }); }; export { CreateSelectSimpleDemo }; //# sourceMappingURL=simple.js.map