UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

390 lines (378 loc) 12.3 kB
// src/components/Button.tsx import clsx from "clsx"; import { jsx, jsxs } from "react/jsx-runtime"; var ButtonSizePaddings = { small: "btn-sm", medium: "btn-md", large: "btn-lg" }; var SolidButton = ({ children, disabled = false, color = "primary", size = "medium", startIcon, endIcon, onClick, className, ...restProps }) => { const colorClasses = { primary: "bg-button-solid-primary-background text-button-solid-primary-text", secondary: "bg-button-solid-secondary-background text-button-solid-secondary-text", tertiary: "bg-button-solid-tertiary-background text-button-solid-tertiary-text", positive: "bg-button-solid-positive-background text-button-solid-positive-text", warning: "bg-button-solid-warning-background text-button-solid-warning-text", negative: "bg-button-solid-negative-background text-button-solid-negative-text" }[color]; const iconColorClasses = { primary: "text-button-solid-primary-icon", secondary: "text-button-solid-secondary-icon", tertiary: "text-button-solid-tertiary-icon", positive: "text-button-solid-positive-icon", warning: "text-button-solid-warning-icon", negative: "text-button-solid-negative-icon" }[color]; return /* @__PURE__ */ jsxs( "button", { onClick: disabled ? void 0 : onClick, disabled: disabled || onClick === void 0, className: clsx( className, { "text-disabled-text bg-disabled-background": disabled, [clsx(colorClasses, "hover:brightness-90")]: !disabled }, ButtonSizePaddings[size] ), ...restProps, children: [ startIcon && /* @__PURE__ */ jsx( "span", { className: clsx({ [iconColorClasses]: !disabled, [`text-disabled-icon`]: disabled }), children: startIcon } ), children, endIcon && /* @__PURE__ */ jsx( "span", { className: clsx({ [iconColorClasses]: !disabled, [`text-disabled-icon`]: disabled }), children: endIcon } ) ] } ); }; // src/hooks/useLanguage.tsx import { createContext, useContext, useEffect as useEffect2, useState as useState2 } from "react"; // src/hooks/useLocalStorage.tsx import { useCallback, useEffect, useState } from "react"; // src/hooks/useLanguage.tsx import { jsx as jsx2 } from "react/jsx-runtime"; var DEFAULT_LANGUAGE = "en"; var LanguageContext = createContext({ language: DEFAULT_LANGUAGE, setLanguage: (v) => v }); var useLanguage = () => useContext(LanguageContext); // src/hooks/useTranslation.ts var useTranslation = (defaults, translationOverwrite = {}) => { const { language: languageProp, translation: overwrite } = translationOverwrite; const { language: inferredLanguage } = useLanguage(); const usedLanguage = languageProp ?? inferredLanguage; let defaultValues = defaults[usedLanguage]; if (overwrite && overwrite[usedLanguage]) { defaultValues = { ...defaultValues, ...overwrite[usedLanguage] }; } return defaultValues; }; // src/components/modals/Modal.tsx import { useContext as useContext2, useEffect as useEffect4 } from "react"; import ReactDOM from "react-dom"; import { X } from "lucide-react"; import clsx3 from "clsx"; // src/hooks/useHoverState.ts import { useEffect as useEffect3, useState as useState3 } from "react"; var defaultUseHoverStateProps = { closingDelay: 200, isDisabled: false }; var useHoverState = (props = void 0) => { const { closingDelay, isDisabled } = { ...defaultUseHoverStateProps, ...props }; const [isHovered, setIsHovered] = useState3(false); const [timer, setTimer] = useState3(); const onMouseEnter = () => { if (isDisabled) { return; } clearTimeout(timer); setIsHovered(true); }; const onMouseLeave = () => { if (isDisabled) { return; } setTimer(setTimeout(() => { setIsHovered(false); }, closingDelay)); }; useEffect3(() => { if (timer) { return () => { clearTimeout(timer); }; } }); useEffect3(() => { if (timer) { clearTimeout(timer); } }, [isDisabled]); return { isHovered, setIsHovered, handlers: { onMouseEnter, onMouseLeave } }; }; // src/components/Tooltip.tsx import { clsx as clsx2 } from "clsx"; import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime"; var Tooltip = ({ tooltip, children, animationDelay = 650, tooltipClassName = "", containerClassName = "", position = "bottom", zIndex = 10 }) => { const { isHovered, handlers } = useHoverState(); const positionClasses = { top: `bottom-full left-1/2 -translate-x-1/2 mb-[6px]`, bottom: `top-full left-1/2 -translate-x-1/2 mt-[6px]`, left: `right-full top-1/2 -translate-y-1/2 mr-[6px]`, right: `left-full top-1/2 -translate-y-1/2 ml-[6px]` }; const triangleSize = 6; const triangleClasses = { top: `top-full left-1/2 -translate-x-1/2 border-t-gray-600 border-l-transparent border-r-transparent`, bottom: `bottom-full left-1/2 -translate-x-1/2 border-b-gray-600 border-l-transparent border-r-transparent`, left: `left-full top-1/2 -translate-y-1/2 border-l-gray-600 border-t-transparent border-b-transparent`, right: `right-full top-1/2 -translate-y-1/2 border-r-gray-600 border-t-transparent border-b-transparent` }; const triangleStyle = { top: { borderWidth: `${triangleSize}px ${triangleSize}px 0 ${triangleSize}px` }, bottom: { borderWidth: `0 ${triangleSize}px ${triangleSize}px ${triangleSize}px` }, left: { borderWidth: `${triangleSize}px 0 ${triangleSize}px ${triangleSize}px` }, right: { borderWidth: `${triangleSize}px ${triangleSize}px ${triangleSize}px 0` } }; return /* @__PURE__ */ jsxs2( "div", { className: clsx2("relative inline-block", containerClassName), ...handlers, children: [ children, isHovered && /* @__PURE__ */ jsxs2( "div", { className: clsx2(`opacity-0 absolute text-black text-xs font-semibold text-gray-600 px-2 py-1 rounded whitespace-nowrap border-2 border-gray-600 animate-tooltip-fade-in shadow-lg bg-gray-100`, positionClasses[position], tooltipClassName), style: { zIndex, animationDelay: animationDelay + "ms" }, children: [ tooltip, /* @__PURE__ */ jsx3( "div", { className: clsx2(`absolute w-0 h-0`, triangleClasses[position]), style: { ...triangleStyle[position], zIndex } } ) ] } ) ] } ); }; // src/components/modals/ModalRegister.tsx import { createContext as createContext2, useState as useState4 } from "react"; // src/util/noop.ts var noop = () => void 0; // src/components/modals/ModalRegister.tsx import { jsx as jsx4 } from "react/jsx-runtime"; var ModalContext = createContext2({ register: [], addToRegister: noop, removeFromRegister: noop }); // src/components/modals/Modal.tsx import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime"; var defaultModalHeaderTranslation = { en: { close: "Close" }, de: { close: "Schlie\xDFen" } }; var ModalHeader = ({ overwriteTranslation, onCloseClick, title, titleText = "", description, descriptionText = "" }) => { const translation = useTranslation(defaultModalHeaderTranslation, overwriteTranslation); const hasTitleRow = !!title || !!titleText || !!onCloseClick; const titleRow = /* @__PURE__ */ jsxs3("div", { className: "row justify-between items-start gap-x-8", children: [ title ?? /* @__PURE__ */ jsx5("span", { className: clsx3("textstyle-title-lg", { "mb-1": description || descriptionText }), children: titleText }), !!onCloseClick && /* @__PURE__ */ jsx5(Tooltip, { tooltip: translation.close, children: /* @__PURE__ */ jsx5("button", { className: "row bg-gray-200 hover:bg-gray-300 rounded-md p-1", onClick: onCloseClick, children: /* @__PURE__ */ jsx5(X, {}) }) }) ] }); return /* @__PURE__ */ jsxs3("div", { className: "col", children: [ hasTitleRow && titleRow, description ?? (descriptionText && /* @__PURE__ */ jsx5("span", { className: "textstyle-description", children: descriptionText })) ] }); }; var modalRootName = "modal-root"; var Modal = ({ children, id, isOpen, onBackgroundClick, backgroundClassName = "", modalClassName = "", containerClassName = "", ...modalHeaderProps }) => { const modalRoot = typeof window !== "undefined" ? document.getElementById(modalRootName) : null; const { register, addToRegister, removeFromRegister } = useContext2(ModalContext); if (!id) { console.error("the id cannot be empty"); } useEffect4(() => { if (isOpen) { addToRegister(id); } else { removeFromRegister(id); } }, [addToRegister, id, removeFromRegister, isOpen]); if (modalRoot === null || !isOpen) return null; const isLast = register.length < 1 || register[register.length - 1] === id; const isSecondLast = register.length < 2 || register[register.length - 2] === id; return ReactDOM.createPortal( /* @__PURE__ */ jsxs3("div", { className: clsx3("fixed inset-0 overflow-y-auto z-[99]", containerClassName), id, children: [ isLast && /* @__PURE__ */ jsx5( "div", { className: clsx3("fixed inset-0 h-screen w-screen", backgroundClassName, { "bg-black/70": isLast && register.length === 1 }), onClick: onBackgroundClick } ), /* @__PURE__ */ jsxs3( "div", { className: clsx3("fixed left-1/2 top-1/2 -translate-y-1/2 -translate-x-1/2 col p-4 bg-white text-black rounded-xl shadow-xl", modalClassName), children: [ /* @__PURE__ */ jsx5(ModalHeader, { ...modalHeaderProps }), children ] } ), !isLast && /* @__PURE__ */ jsx5( "div", { className: clsx3("fixed inset-0 h-screen w-screen", backgroundClassName, { "bg-black/70": isSecondLast && register.length > 1 }) } ) ] }), modalRoot, id ); }; // src/components/modals/ConfirmDialog.tsx import { jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime"; var defaultConfirmDialogTranslation = { en: { confirm: "Confirm", cancel: "Cancel", decline: "Decline" }, de: { confirm: "Best\xE4tigen", cancel: "Abbrechen", decline: "Ablehnen" } }; var ConfirmDialog = ({ overwriteTranslation, children, onCancel, onConfirm, onDecline, confirmType = "positive", buttonOverwrites, ...restProps }) => { const translation = useTranslation(defaultConfirmDialogTranslation, overwriteTranslation); const mapping = { neutral: "primary", negative: "negative", positive: "positive" }; return /* @__PURE__ */ jsxs4(Modal, { ...restProps, children: [ children, /* @__PURE__ */ jsxs4("div", { className: "row mt-3 gap-x-4 justify-end", children: [ onCancel && /* @__PURE__ */ jsx6( SolidButton, { color: buttonOverwrites?.[0].color ?? "primary", onClick: onCancel, disabled: buttonOverwrites?.[0].disabled ?? false, children: buttonOverwrites?.[0].text ?? translation.cancel } ), onDecline && /* @__PURE__ */ jsx6( SolidButton, { color: buttonOverwrites?.[1].color ?? "negative", onClick: onDecline, disabled: buttonOverwrites?.[1].disabled ?? false, children: buttonOverwrites?.[1].text ?? translation.decline } ), /* @__PURE__ */ jsx6( SolidButton, { autoFocus: true, color: buttonOverwrites?.[2].color ?? mapping[confirmType], onClick: onConfirm, disabled: buttonOverwrites?.[2].disabled ?? false, children: buttonOverwrites?.[2].text ?? translation.confirm } ) ] }) ] }); }; export { ConfirmDialog }; //# sourceMappingURL=ConfirmDialog.mjs.map