UNPKG

@sikka/hawa

Version:

Modern UI Kit made with Tailwind

277 lines (269 loc) • 9.36 kB
"use client"; // elements/colorPicker/ColorPicker.tsx import React5, { useState, useEffect } from "react"; // util/index.ts import { clsx } from "clsx"; import { twMerge } from "tailwind-merge"; function cn(...inputs) { return twMerge(clsx(inputs)); } var parseColor = (color) => { if (color.startsWith("#")) { let r = parseInt(color.slice(1, 3), 16); let g = parseInt(color.slice(3, 5), 16); let b = parseInt(color.slice(5, 7), 16); return [r, g, b]; } else if (color.startsWith("rgb")) { return color.match(/\d+/g).map(Number); } return [255, 255, 255]; }; var calculateLuminance = (color) => { var _a; const [r, g, b] = (_a = parseColor(color)) == null ? void 0 : _a.map((c) => { c /= 255; return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; }); return 0.2126 * r + 0.7152 * g + 0.0722 * b; }; // elements/skeleton/Skeleton.tsx import React from "react"; function Skeleton({ className, content, animation = "pulse", fade, ...props }) { const animationStyles = { none: "hawa-rounded hawa-bg-muted", pulse: "hawa-animate-pulse hawa-rounded hawa-bg-muted", shimmer: "hawa-space-y-5 hawa-rounded hawa-bg-muted hawa-p-4 hawa-relative before:hawa-absolute before:hawa-inset-0 before:hawa--translate-x-full before:hawa-animate-[shimmer_2s_infinite] before:hawa-bg-gradient-to-r before:hawa-from-transparent before:hawa-via-gray-300/40 dark:before:hawa-via-white/10 before:hawa-to-transparent hawa-isolate hawa-overflow-hidden before:hawa-border-t before:hawa-border-rose-100/10" }; const fadeStyle = { bottom: "hawa-mask-fade-bottom", top: "hawa-mask-fade-top", right: "hawa-mask-fade-right", left: "hawa-mask-fade-left " }; return /* @__PURE__ */ React.createElement( "div", { className: cn( animationStyles[animation], content && "hawa-flex hawa-flex-col hawa-items-center hawa-justify-center", fade && fadeStyle[fade], className ), ...props }, content && content ); } // elements/helperText/HelperText.tsx import React2 from "react"; var HelperText = ({ helperText }) => /* @__PURE__ */ React2.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 React4 from "react"; // elements/tooltip/Tooltip.tsx import React3 from "react"; import * as TooltipPrimitive from "@radix-ui/react-tooltip"; var TooltipContent = React3.forwardRef(({ className, sideOffset = 4, size = "default", ...props }, ref) => /* @__PURE__ */ React3.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 = React3.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ React3.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__ */ React3.createElement( TooltipPrimitive.TooltipProvider, { delayDuration, ...providerProps }, /* @__PURE__ */ React3.createElement( TooltipPrimitive.Root, { open: !disabled && open, defaultOpen, onOpenChange, ...props }, /* @__PURE__ */ React3.createElement(TooltipPrimitive.Trigger, { ...triggerProps }, children), /* @__PURE__ */ React3.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 = React4.forwardRef(({ className, hint, hintSide, required, children, ...props }, ref) => /* @__PURE__ */ React4.createElement("div", { className: "hawa-flex hawa-flex-row hawa-items-center hawa-gap-1 hawa-transition-all" }, /* @__PURE__ */ React4.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__ */ React4.createElement("span", { className: "hawa-mx-0.5 hawa-text-red-500" }, "*") ), hint && /* @__PURE__ */ React4.createElement( Tooltip, { content: hint, side: hintSide, triggerProps: { tabIndex: -1, onClick: (event) => event.preventDefault() } }, /* @__PURE__ */ React4.createElement("div", null, /* @__PURE__ */ React4.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__ */ React4.createElement("circle", { cx: "12", cy: "12", r: "10" }), /* @__PURE__ */ React4.createElement("path", { d: "M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3" }), /* @__PURE__ */ React4.createElement("path", { d: "M12 17h.01" }) )) ))); Label.displayName = "Label"; // elements/colorPicker/ColorPicker.tsx var ColorPicker = ({ containerProps, colorPickerProps, textInputProps, labelProps, forceHideHelperText, isLoading, preview = false, ...props }) => { const [selectedColor, setSelectedColor] = useState(props.color); useEffect(() => { if (selectedColor && selectedColor[0] !== "#") { setSelectedColor(`#${selectedColor}`); } }, [selectedColor]); const handleTextInputChange = (e) => { const inputElement = e.target; let inputColor = inputElement.value; if (inputColor[0] !== "#") { inputColor = `#${inputColor}`; } const sanitizedInput = inputColor.replace(/[^a-fA-F0-9#]/g, ""); setSelectedColor(sanitizedInput); if (props.handleChange) { props.handleChange(e); } }; return /* @__PURE__ */ React5.createElement("div", { className: "hawa-flex hawa-w-fit hawa-flex-col hawa-gap-2" }, props.label && /* @__PURE__ */ React5.createElement(Label, { ...labelProps }, props.label), isLoading ? /* @__PURE__ */ React5.createElement(Skeleton, { style: { height: 40, width: 148 } }) : /* @__PURE__ */ React5.createElement("div", { dir: "ltr", className: "hawa-flex hawa-w-full hawa-flex-row" }, /* @__PURE__ */ React5.createElement( "div", { style: { height: 40, backgroundColor: selectedColor }, className: "hawa-rounded-bl-lg hawa-rounded-tl-lg hawa-border" }, /* @__PURE__ */ React5.createElement( "input", { disabled: preview, type: "color", value: selectedColor, onChange: (e) => { setSelectedColor(e.target.value); if (props.handleChange) { props.handleChange(e); } }, className: cn( "hawa-mt-0 hawa-h-[38px] hawa-opacity-0", props.colorPickerClassNames ), ...colorPickerProps } ) ), /* @__PURE__ */ React5.createElement("div", { className: "hawa-relative hawa-flex hawa-max-h-fit hawa-w-full hawa-flex-col hawa-justify-center hawa-gap-0" }, /* @__PURE__ */ React5.createElement( "input", { disabled: preview, maxLength: 7, type: "text", onInput: handleTextInputChange, value: selectedColor, className: cn( "hawa-block hawa-h-[40px] hawa-w-24 hawa-rounded hawa-rounded-l-none hawa-bg-background hawa-p-2 hawa-text-sm hawa-transition-all", "hawa-border hawa-border-l-0 hawa-border-l-transparent placeholder:hawa-text-muted-foreground" // "hawa-border hawa-border-x-0 hawa-border-x-transparent hawa-border-b-0 hawa-rounded-tr-none" ), style: { backgroundColor: preview ? selectedColor : "hsl(var(--background))", color: preview ? calculateLuminance(selectedColor) > 0.5 ? "black" : "white" : "" }, ...textInputProps } ))), !forceHideHelperText && /* @__PURE__ */ React5.createElement(HelperText, { helperText: props.helperText })); }; export { ColorPicker }; //# sourceMappingURL=index.mjs.map