UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

600 lines (582 loc) 20.4 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/components/modals/InputModal.tsx var InputModal_exports = {}; __export(InputModal_exports, { InputModal: () => InputModal }); module.exports = __toCommonJS(InputModal_exports); // src/components/user-input/Input.tsx var import_react2 = require("react"); var import_clsx = __toESM(require("clsx")); // src/hooks/useSaveDelay.ts var import_react = require("react"); function useSaveDelay(setNotificationStatus, delay) { const [updateTimer, setUpdateTimer] = (0, import_react.useState)(void 0); const [notificationTimer, setNotificationTimer] = (0, import_react.useState)(void 0); const restartTimer = (onSave) => { clearTimeout(updateTimer); setUpdateTimer(setTimeout(() => { onSave(); setNotificationStatus(true); clearTimeout(notificationTimer); setNotificationTimer(setTimeout(() => { setNotificationStatus(false); clearTimeout(notificationTimer); }, delay)); clearTimeout(updateTimer); }, delay)); }; const clearUpdateTimer = (hasSaved = true) => { clearTimeout(updateTimer); if (hasSaved) { setNotificationStatus(true); clearTimeout(notificationTimer); setNotificationTimer(setTimeout(() => { setNotificationStatus(false); clearTimeout(notificationTimer); }, delay)); } else { setNotificationStatus(false); } }; (0, import_react.useEffect)(() => { return () => { clearTimeout(updateTimer); clearTimeout(notificationTimer); }; }, []); return { restartTimer, clearUpdateTimer }; } // src/util/noop.ts var noop = () => void 0; // src/components/user-input/Label.tsx var import_jsx_runtime = require("react/jsx-runtime"); var styleMapping = { labelSmall: "textstyle-label-sm", labelMedium: "textstyle-label-md", labelBig: "textstyle-label-lg" }; var Label = ({ children, name, labelType = "labelSmall", ...props }) => { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", { ...props, children: children ? children : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: styleMapping[labelType], children: name }) }); }; // src/components/user-input/Input.tsx var import_jsx_runtime2 = require("react/jsx-runtime"); var ControlledInput = ({ id, type = "text", value, label, onChange = noop, onChangeEvent = noop, className = "", onEditCompleted, expanded = true, onBlur, containerClassName, ...restProps }) => { const { restartTimer, clearUpdateTimer } = useSaveDelay(() => void 0, 3e3); const ref = (0, import_react2.useRef)(null); (0, import_react2.useEffect)(() => { if (restProps.autoFocus) { ref.current?.focus(); } }, [restProps.autoFocus]); return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: (0, import_clsx.default)({ "w-full": expanded }, containerClassName), children: [ label && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { ...label, htmlFor: id, className: (0, import_clsx.default)("mb-1", label.className) }), /* @__PURE__ */ (0, import_jsx_runtime2.jsx)( "input", { ref, value, id, type, className: (0, import_clsx.default)("block bg-surface text-on-surface px-3 py-2 rounded-md w-full border-2 border-gray-200 hover:border-primary focus:outline-none focus:border-primary focus:ring-primary", className), onBlur: (event) => { if (onBlur) { onBlur(event); } if (onEditCompleted) { onEditCompleted(event.target.value, event); clearUpdateTimer(); } }, onChange: (e) => { const value2 = e.target.value; if (onEditCompleted) { restartTimer(() => { onEditCompleted(value2, e); clearUpdateTimer(); }); } onChange(value2, e); onChangeEvent(e); }, ...restProps } ) ] }); }; var FormInput = (0, import_react2.forwardRef)(function FormInput2({ id, labelText, errorText, className, labelClassName, errorClassName, containerClassName, required, ...restProps }, ref) { const input = /* @__PURE__ */ (0, import_jsx_runtime2.jsx)( "input", { ref, id, ...restProps, className: (0, import_clsx.default)( "block bg-surface text-on-surface px-3 py-2 rounded-md w-full border-2 border-gray-200 hover:border-primary focus:outline-none focus:border-primary focus:ring-primary", { "focus:border-primary focus:ring-primary": !errorText, "focus:border-negative focus:ring-negative text-negative": !!errorText }, className ) } ); return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: (0, import_clsx.default)("flex flex-col gap-y-1", containerClassName), children: [ labelText && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("label", { htmlFor: id, className: (0, import_clsx.default)("textstyle-label-md", labelClassName), children: [ labelText, required && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "text-primary font-bold", children: "*" }) ] }), input, errorText && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("label", { htmlFor: id, className: (0, import_clsx.default)("text-negative", errorClassName), children: errorText }) ] }); }); // src/components/Button.tsx var import_clsx2 = __toESM(require("clsx")); var import_jsx_runtime3 = require("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__ */ (0, import_jsx_runtime3.jsxs)( "button", { onClick: disabled ? void 0 : onClick, disabled: disabled || onClick === void 0, className: (0, import_clsx2.default)( className, { "text-disabled-text bg-disabled-background": disabled, [(0, import_clsx2.default)(colorClasses, "hover:brightness-90")]: !disabled }, ButtonSizePaddings[size] ), ...restProps, children: [ startIcon && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)( "span", { className: (0, import_clsx2.default)({ [iconColorClasses]: !disabled, [`text-disabled-icon`]: disabled }), children: startIcon } ), children, endIcon && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)( "span", { className: (0, import_clsx2.default)({ [iconColorClasses]: !disabled, [`text-disabled-icon`]: disabled }), children: endIcon } ) ] } ); }; // src/hooks/useLanguage.tsx var import_react4 = require("react"); // src/hooks/useLocalStorage.tsx var import_react3 = require("react"); // src/hooks/useLanguage.tsx var import_jsx_runtime4 = require("react/jsx-runtime"); var DEFAULT_LANGUAGE = "en"; var LanguageContext = (0, import_react4.createContext)({ language: DEFAULT_LANGUAGE, setLanguage: (v) => v }); var useLanguage = () => (0, import_react4.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 var import_react7 = require("react"); var import_react_dom = __toESM(require("react-dom")); var import_lucide_react = require("lucide-react"); var import_clsx4 = __toESM(require("clsx")); // src/hooks/useHoverState.ts var import_react5 = require("react"); var defaultUseHoverStateProps = { closingDelay: 200, isDisabled: false }; var useHoverState = (props = void 0) => { const { closingDelay, isDisabled } = { ...defaultUseHoverStateProps, ...props }; const [isHovered, setIsHovered] = (0, import_react5.useState)(false); const [timer, setTimer] = (0, import_react5.useState)(); const onMouseEnter = () => { if (isDisabled) { return; } clearTimeout(timer); setIsHovered(true); }; const onMouseLeave = () => { if (isDisabled) { return; } setTimer(setTimeout(() => { setIsHovered(false); }, closingDelay)); }; (0, import_react5.useEffect)(() => { if (timer) { return () => { clearTimeout(timer); }; } }); (0, import_react5.useEffect)(() => { if (timer) { clearTimeout(timer); } }, [isDisabled]); return { isHovered, setIsHovered, handlers: { onMouseEnter, onMouseLeave } }; }; // src/components/Tooltip.tsx var import_clsx3 = require("clsx"); var import_jsx_runtime5 = require("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__ */ (0, import_jsx_runtime5.jsxs)( "div", { className: (0, import_clsx3.clsx)("relative inline-block", containerClassName), ...handlers, children: [ children, isHovered && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)( "div", { className: (0, import_clsx3.clsx)(`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__ */ (0, import_jsx_runtime5.jsx)( "div", { className: (0, import_clsx3.clsx)(`absolute w-0 h-0`, triangleClasses[position]), style: { ...triangleStyle[position], zIndex } } ) ] } ) ] } ); }; // src/components/modals/ModalRegister.tsx var import_react6 = require("react"); var import_jsx_runtime6 = require("react/jsx-runtime"); var ModalContext = (0, import_react6.createContext)({ register: [], addToRegister: noop, removeFromRegister: noop }); // src/components/modals/Modal.tsx var import_jsx_runtime7 = require("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__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "row justify-between items-start gap-x-8", children: [ title ?? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: (0, import_clsx4.default)("textstyle-title-lg", { "mb-1": description || descriptionText }), children: titleText }), !!onCloseClick && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Tooltip, { tooltip: translation.close, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { className: "row bg-gray-200 hover:bg-gray-300 rounded-md p-1", onClick: onCloseClick, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_lucide_react.X, {}) }) }) ] }); return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "col", children: [ hasTitleRow && titleRow, description ?? (descriptionText && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("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 } = (0, import_react7.useContext)(ModalContext); if (!id) { console.error("the id cannot be empty"); } (0, import_react7.useEffect)(() => { 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 import_react_dom.default.createPortal( /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: (0, import_clsx4.default)("fixed inset-0 overflow-y-auto z-[99]", containerClassName), id, children: [ isLast && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)( "div", { className: (0, import_clsx4.default)("fixed inset-0 h-screen w-screen", backgroundClassName, { "bg-black/70": isLast && register.length === 1 }), onClick: onBackgroundClick } ), /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)( "div", { className: (0, import_clsx4.default)("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__ */ (0, import_jsx_runtime7.jsx)(ModalHeader, { ...modalHeaderProps }), children ] } ), !isLast && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)( "div", { className: (0, import_clsx4.default)("fixed inset-0 h-screen w-screen", backgroundClassName, { "bg-black/70": isSecondLast && register.length > 1 }) } ) ] }), modalRoot, id ); }; // src/components/modals/ConfirmDialog.tsx var import_jsx_runtime8 = require("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__ */ (0, import_jsx_runtime8.jsxs)(Modal, { ...restProps, children: [ children, /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "row mt-3 gap-x-4 justify-end", children: [ onCancel && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)( SolidButton, { color: buttonOverwrites?.[0].color ?? "primary", onClick: onCancel, disabled: buttonOverwrites?.[0].disabled ?? false, children: buttonOverwrites?.[0].text ?? translation.cancel } ), onDecline && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)( SolidButton, { color: buttonOverwrites?.[1].color ?? "negative", onClick: onDecline, disabled: buttonOverwrites?.[1].disabled ?? false, children: buttonOverwrites?.[1].text ?? translation.decline } ), /* @__PURE__ */ (0, import_jsx_runtime8.jsx)( SolidButton, { autoFocus: true, color: buttonOverwrites?.[2].color ?? mapping[confirmType], onClick: onConfirm, disabled: buttonOverwrites?.[2].disabled ?? false, children: buttonOverwrites?.[2].text ?? translation.confirm } ) ] }) ] }); }; // src/components/modals/InputModal.tsx var import_jsx_runtime9 = require("react/jsx-runtime"); var InputModal = ({ inputs, buttonOverwrites, ...restProps }) => { return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)( ConfirmDialog, { buttonOverwrites, ...restProps, children: inputs.map((inputProps, index) => /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(ControlledInput, { ...inputProps }, `input ${index}`)) } ); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { InputModal }); //# sourceMappingURL=InputModal.js.map