UNPKG

@sikka/hawa

Version:

Modern UI Kit made with Tailwind

433 lines (425 loc) • 16.8 kB
"use client"; // elements/radio/Radio.tsx import React5, { useState, useRef, useEffect, forwardRef as forwardRef3 } from "react"; // util/index.ts import { clsx } from "clsx"; import { twMerge } from "tailwind-merge"; function cn(...inputs) { return twMerge(clsx(inputs)); } // elements/helperText/HelperText.tsx import React from "react"; var HelperText = ({ helperText }) => /* @__PURE__ */ React.createElement( "p", { className: cn( "hawa-my-0 hawa-text-start hawa-text-xs hawa-text-helper-color hawa-transition-all", helperText ? "hawa-h-4 hawa-opacity-100" : "hawa-h-0 hawa-opacity-0" ) }, helperText ); // elements/label/Label.tsx import * as React3 from "react"; // elements/tooltip/Tooltip.tsx import React2 from "react"; import * as TooltipPrimitive from "@radix-ui/react-tooltip"; var TooltipContent = React2.forwardRef(({ className, sideOffset = 4, size = "default", ...props }, ref) => /* @__PURE__ */ React2.createElement( TooltipPrimitive.Content, { ref, sideOffset, className: cn( "hawa-z-50 hawa-overflow-hidden hawa-rounded-md hawa-border hawa-bg-popover hawa-px-3 hawa-py-1.5 hawa-text-sm hawa-text-popover-foreground hawa-shadow-md hawa-animate-in hawa-fade-in-0 hawa-zoom-in-95 data-[state=closed]:hawa-animate-out data-[state=closed]:hawa-fade-out-0 data-[state=closed]:hawa-zoom-out-95 data-[side=bottom]:hawa-slide-in-from-top-2 data-[side=left]:hawa-slide-in-from-right-2 data-[side=right]:hawa-slide-in-from-left-2 data-[side=top]:hawa-slide-in-from-bottom-2", { "hawa-text-xs": size === "small", "hawa-text-xl": size === "large" }, className ), ...props } )); TooltipContent.displayName = TooltipPrimitive.Content.displayName; var TooltipArrow = React2.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ React2.createElement(TooltipPrimitive.Arrow, { ref, className: cn(className), ...props })); TooltipArrow.displayName = TooltipPrimitive.Arrow.displayName; var Tooltip = ({ side, size, open, content, children, disabled, defaultOpen, onOpenChange, triggerProps, contentProps, providerProps, delayDuration = 300, ...props }) => { return /* @__PURE__ */ React2.createElement( TooltipPrimitive.TooltipProvider, { delayDuration, ...providerProps }, /* @__PURE__ */ React2.createElement( TooltipPrimitive.Root, { open: !disabled && open, defaultOpen, onOpenChange, ...props }, /* @__PURE__ */ React2.createElement(TooltipPrimitive.Trigger, { ...triggerProps }, children), /* @__PURE__ */ React2.createElement( TooltipContent, { size, side, align: "center", ...contentProps, style: { ...contentProps == null ? void 0 : contentProps.style, maxWidth: "var(--radix-tooltip-content-available-width)", maxHeight: "var(--radix-tooltip-content-available-height)" } }, content ) ) ); }; // elements/label/Label.tsx var Label = React3.forwardRef(({ className, hint, hintSide, required, children, ...props }, ref) => /* @__PURE__ */ React3.createElement("div", { className: "hawa-flex hawa-flex-row hawa-items-center hawa-gap-1 hawa-transition-all" }, /* @__PURE__ */ React3.createElement( "label", { ref, className: cn( "hawa-text-sm hawa-font-medium hawa-leading-none peer-disabled:hawa-cursor-not-allowed peer-disabled:hawa-opacity-70", className ), ...props }, children, required && /* @__PURE__ */ React3.createElement("span", { className: "hawa-mx-0.5 hawa-text-red-500" }, "*") ), hint && /* @__PURE__ */ React3.createElement( Tooltip, { content: hint, side: hintSide, triggerProps: { tabIndex: -1, onClick: (event) => event.preventDefault() } }, /* @__PURE__ */ React3.createElement("div", null, /* @__PURE__ */ React3.createElement( "svg", { xmlns: "http://www.w3.org/2000/svg", className: "hawa-h-[14px] hawa-w-[14px] hawa-cursor-help", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round" }, /* @__PURE__ */ React3.createElement("circle", { cx: "12", cy: "12", r: "10" }), /* @__PURE__ */ React3.createElement("path", { d: "M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3" }), /* @__PURE__ */ React3.createElement("path", { d: "M12 17h.01" }) )) ))); Label.displayName = "Label"; // elements/popover/Popover.tsx import * as React4 from "react"; import * as PopoverPrimitive from "@radix-ui/react-popover"; var PopoverContent = React4.forwardRef( ({ className, align = "center", sideOffset = 4, container, ...props }, ref) => /* @__PURE__ */ React4.createElement(PopoverPrimitive.Portal, { container }, /* @__PURE__ */ React4.createElement( PopoverPrimitive.Content, { ref, align, sideOffset, className: cn( "dark:dark-shadow hawa-z-50 hawa-rounded hawa-border hawa-bg-popover hawa-text-popover-foreground hawa-shadow-md hawa-outline-none data-[state=open]:hawa-animate-in data-[state=closed]:hawa-animate-out data-[state=closed]:hawa-fade-out-0 data-[state=open]:hawa-fade-in-0 data-[state=closed]:hawa-zoom-out-95 data-[state=open]:hawa-zoom-in-95 data-[side=bottom]:hawa-slide-in-from-top-2 data-[side=left]:hawa-slide-in-from-right-2 data-[side=right]:hawa-slide-in-from-left-2 data-[side=top]:hawa-slide-in-from-bottom-2", className ), ...props } )) ); PopoverContent.displayName = PopoverPrimitive.Content.displayName; var PopoverTrigger = PopoverPrimitive.Trigger; var PopoverRoot = PopoverPrimitive.Root; // elements/radio/Radio.tsx var Radio = forwardRef3( ({ design = "default", width = "default", size = "default", orientation = "horizontal", name, labelProps, tabsContainerClassName, forceHideHelperText = false, onChange, containerClassNames, ...props }, ref) => { var _a, _b, _c; let activeTabStyle = "hawa-inline-block hawa-w-full hawa-text-primary-foreground hawa-bg-primary hawa-active dark:hawa-bg-primary"; let inactiveTabStyle = `hawa-inline-block hawa-w-full hawa-transition-all hawa-bg-primary-foreground dark:hover:hawa-text-white ${props.disabled ? "" : "hover:hawa-bg-muted"}`; let orientationStyle = { horizontal: "hawa-flex hawa-flex-row", vertical: "hawa-flex hawa-flex-col" }; let tabSizeStyle = { default: "hawa-py-2 hawa-px-4 hawa-text-sm", lg: "hawa-py-2 hawa-px-4", sm: "hawa-p-1.5 hawa-text-xs", xs: "hawa-p-1 hawa-text-[10px]" }; let widthStyle = { none: "", default: "hawa-max-w-fit", full: "hawa-w-full" }; const [parentDirection, setParentDirection] = React5.useState( null ); const [selectedOption, setSelectedOption] = useState( props.defaultValue || props.value ); const [openTooltip, setOpenTooltip] = useState(null); const parentRef = useRef(null); useEffect(() => { var _a2; const parentNode = (_a2 = parentRef.current) == null ? void 0 : _a2.parentNode; if (parentNode) { const dir = window.getComputedStyle(parentNode).direction; setParentDirection(dir); } }); const handleChange = (opt) => { setSelectedOption(opt.value); if (onChange) { onChange(opt.value); } else { console.log("onChange was not provided"); } }; const radio_option_tabs_styling = [ "hawa-w-full hawa-last hawa-flex hawa-flex-row hawa-items-center hawa-justify-center hawa-gap-2 ", !props.disabled && "hawa-cursor-pointer", orientation === "horizontal" && parentDirection === "ltr" && "hawa-rounded-none first:hawa-rounded-l last:hawa-rounded-r", orientation === "horizontal" && parentDirection === "rtl" && "hawa-rounded-none first:hawa-rounded-r last:hawa-rounded-l", orientation === "vertical" && "hawa-rounded-none first:hawa-rounded-t last:hawa-rounded-b", tabSizeStyle[size] ]; switch (design) { case "tabs": return /* @__PURE__ */ React5.createElement( "div", { className: cn( "hawa-gap-2 hawa-flex hawa-flex-col", containerClassNames == null ? void 0 : containerClassNames.tabs ) }, props.label && /* @__PURE__ */ React5.createElement(Label, { ...labelProps }, props.label), /* @__PURE__ */ React5.createElement( "ul", { ref: parentRef, className: cn( props.options && ((_a = props.options) == null ? void 0 : _a.length) > 2 ? "hawa-flex-wrap xs:hawa-max-w-full xs:hawa-flex-nowrap" : "", "hawa-select-none hawa-whitespace-nowrap hawa-rounded hawa-border hawa-text-center hawa-font-medium hawa-h-[40px]", orientationStyle[orientation], widthStyle[width], tabsContainerClassName ) }, (_b = props.options) == null ? void 0 : _b.map((opt, o) => { return opt.tooltip ? /* @__PURE__ */ React5.createElement( PopoverRoot, { key: o, open: o === openTooltip, onOpenChange: (bool) => setOpenTooltip(bool ? o : null) }, /* @__PURE__ */ React5.createElement( PopoverTrigger, { onMouseEnter: () => setOpenTooltip(o), onMouseLeave: () => setOpenTooltip(null), asChild: true }, /* @__PURE__ */ React5.createElement( "li", { "aria-current": "page", onClick: () => { if (props.disabled || opt.disabled) return; handleChange(opt); }, className: cn( ...radio_option_tabs_styling, selectedOption === opt.value ? activeTabStyle : inactiveTabStyle ) }, opt.icon && opt.icon, opt.label ) ), /* @__PURE__ */ React5.createElement(PopoverContent, { ...opt.tooltipContentProps }, opt.tooltip) ) : /* @__PURE__ */ React5.createElement( "li", { key: o, "aria-current": "page", onClick: () => { if (props.disabled || opt.disabled) return; handleChange(opt); }, className: cn( ...radio_option_tabs_styling, selectedOption === opt.value ? activeTabStyle : inactiveTabStyle ) }, opt.icon && opt.icon, opt.label ); }) ), !forceHideHelperText && /* @__PURE__ */ React5.createElement(HelperText, { helperText: props.helperText }) ); case "bordered": return /* @__PURE__ */ React5.createElement( "div", { className: cn( orientationStyle[orientation], "hawa-gap-4", containerClassNames == null ? void 0 : containerClassNames.bordered ) }, props.options && props.options.map((opt, i) => /* @__PURE__ */ React5.createElement("div", { key: i, className: "hawa-w-full hawa-rounded hawa-border" }, /* @__PURE__ */ React5.createElement( "div", { className: cn( "radio-item radio-item-bordered hawa-flex hawa-items-center hawa-transition-all", props.direction === "rtl" ? "margin-left right-19px" : "margin-right left-23px" ), key: i + 1 }, /* @__PURE__ */ React5.createElement( "input", { disabled: opt.disabled, id: opt.value.toString(), type: "radio", value: opt.value, name, onChange: () => handleChange(opt) } ), /* @__PURE__ */ React5.createElement( "label", { htmlFor: opt.value.toString(), className: cn( "hawa-ml-2 hawa-w-full hawa-select-none hawa-p-4 hawa-pl-3 hawa-text-sm hawa-font-medium hawa-text-black dark:hawa-text-white", opt.disabled ? "hawa-opacity-50" : "hawa-cursor-pointer hawa-text-gray-900" ) }, opt.label ) ))) ); case "cards": return /* @__PURE__ */ React5.createElement( "ul", { className: cn( orientationStyle[orientation], "hawa-gap-4", containerClassNames == null ? void 0 : containerClassNames.cards ) }, (_c = props.options) == null ? void 0 : _c.map((opt, o) => /* @__PURE__ */ React5.createElement("li", { key: o, onClick: () => handleChange(opt) }, /* @__PURE__ */ React5.createElement( "input", { type: "radio", id: opt.value.toString(), name, value: opt.value.toString(), className: "hawa-peer hawa-hidden", required: true, disabled: opt.disabled } ), /* @__PURE__ */ React5.createElement( "label", { htmlFor: opt.value.toString(), className: cn( "hawa-inline-flex hawa-h-full hawa-w-full hawa-transition-all hawa-items-center hawa-justify-between hawa-rounded-lg hawa-border hawa-border-foreground/10 hawa-bg-background hawa-p-5 hawa-text-gray-500 peer-checked:hawa-border-primary peer-checked:hawa-text-primary dark:hawa-border-foreground/10 dark:hawa-bg-foreground/5 dark:hawa-text-gray-400 dark:peer-checked:hawa-text-primary", opt.disabled ? "hawa-opacity-50" : "hawa-cursor-pointer hover:hawa-bg-foreground/10 hover:hawa-text-gray-600 dark:hover:hawa-bg-foreground/20 dark:hover:hawa-text-gray-300" ) }, /* @__PURE__ */ React5.createElement("div", { className: "hawa-block hawa-h-full hawa-w-full" }, /* @__PURE__ */ React5.createElement("div", { className: "hawa-w-full hawa-text-lg hawa-font-semibold" }, opt.label), /* @__PURE__ */ React5.createElement("div", { className: "hawa-w-full" }, opt.sublabel)) ))) ); default: return /* @__PURE__ */ React5.createElement( "div", { className: cn( "hawa-flex hawa-flex-col hawa-gap-2", containerClassNames == null ? void 0 : containerClassNames.default ) }, props.label && /* @__PURE__ */ React5.createElement(Label, { ...labelProps }, props.label), /* @__PURE__ */ React5.createElement("div", { className: cn(orientationStyle[orientation], "hawa-gap-2") }, props.options && props.options.map((opt, i) => /* @__PURE__ */ React5.createElement( "div", { className: cn( "radio-item radio-item-default hawa-flex hawa-items-center hawa-transition-all", props.direction === "rtl" ? "margin-left right-3px" : "margin-right left-3px" ), key: i + 1 }, /* @__PURE__ */ React5.createElement( "input", { disabled: opt.disabled, id: opt.value.toString(), type: "radio", value: opt.value, name, onChange: () => handleChange(opt) } ), /* @__PURE__ */ React5.createElement( "label", { htmlFor: opt.value.toString(), className: cn( "hawa-text-sm hawa-font-medium dark:hawa-text-white", opt.disabled ? "hawa-text-gray-400" : "hawa-cursor-pointer hawa-text-gray-900" ) }, opt.label ) ))), /* @__PURE__ */ React5.createElement(HelperText, { helperText: props.helperText }) ); } } ); export { Radio }; //# sourceMappingURL=index.mjs.map