@helpwave/hightide
Version:
helpwave's component and theming library
568 lines (552 loc) • 17.4 kB
JavaScript
// src/components/user-input/Input.tsx
import {
useEffect as useEffect2,
useRef,
useState as useState2,
forwardRef
} from "react";
import clsx from "clsx";
// src/hooks/useSaveDelay.ts
import { useEffect, useState } from "react";
function useSaveDelay(setNotificationStatus, delay) {
const [updateTimer, setUpdateTimer] = useState(void 0);
const [notificationTimer, setNotificationTimer] = 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);
}
};
useEffect(() => {
return () => {
clearTimeout(updateTimer);
clearTimeout(notificationTimer);
};
}, []);
return { restartTimer, clearUpdateTimer };
}
// src/util/noop.ts
var noop = () => void 0;
// src/components/user-input/Label.tsx
import { jsx } from "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__ */ jsx("label", { ...props, children: children ? children : /* @__PURE__ */ jsx("span", { className: styleMapping[labelType], children: name }) });
};
// src/components/user-input/Input.tsx
import { jsx as jsx2, jsxs } from "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 = useRef(null);
useEffect2(() => {
if (restProps.autoFocus) {
ref.current?.focus();
}
}, [restProps.autoFocus]);
return /* @__PURE__ */ jsxs("div", { className: clsx({ "w-full": expanded }, containerClassName), children: [
label && /* @__PURE__ */ jsx2(Label, { ...label, htmlFor: id, className: clsx("mb-1", label.className) }),
/* @__PURE__ */ jsx2(
"input",
{
ref,
value,
id,
type,
className: clsx("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 = forwardRef(function FormInput2({
id,
labelText,
errorText,
className,
labelClassName,
errorClassName,
containerClassName,
required,
...restProps
}, ref) {
const input = /* @__PURE__ */ jsx2(
"input",
{
ref,
id,
...restProps,
className: clsx(
"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__ */ jsxs("div", { className: clsx("flex flex-col gap-y-1", containerClassName), children: [
labelText && /* @__PURE__ */ jsxs("label", { htmlFor: id, className: clsx("textstyle-label-md", labelClassName), children: [
labelText,
required && /* @__PURE__ */ jsx2("span", { className: "text-primary font-bold", children: "*" })
] }),
input,
errorText && /* @__PURE__ */ jsx2("label", { htmlFor: id, className: clsx("text-negative", errorClassName), children: errorText })
] });
});
// src/components/Button.tsx
import clsx2 from "clsx";
import { jsx as jsx3, jsxs as jsxs2 } 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__ */ jsxs2(
"button",
{
onClick: disabled ? void 0 : onClick,
disabled: disabled || onClick === void 0,
className: clsx2(
className,
{
"text-disabled-text bg-disabled-background": disabled,
[clsx2(colorClasses, "hover:brightness-90")]: !disabled
},
ButtonSizePaddings[size]
),
...restProps,
children: [
startIcon && /* @__PURE__ */ jsx3(
"span",
{
className: clsx2({
[iconColorClasses]: !disabled,
[`text-disabled-icon`]: disabled
}),
children: startIcon
}
),
children,
endIcon && /* @__PURE__ */ jsx3(
"span",
{
className: clsx2({
[iconColorClasses]: !disabled,
[`text-disabled-icon`]: disabled
}),
children: endIcon
}
)
]
}
);
};
// src/hooks/useLanguage.tsx
import { createContext, useContext, useEffect as useEffect4, useState as useState4 } from "react";
// src/hooks/useLocalStorage.tsx
import { useCallback, useEffect as useEffect3, useState as useState3 } from "react";
// src/hooks/useLanguage.tsx
import { jsx as jsx4 } 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 useEffect6 } from "react";
import ReactDOM from "react-dom";
import { X } from "lucide-react";
import clsx4 from "clsx";
// src/hooks/useHoverState.ts
import { useEffect as useEffect5, useState as useState5 } from "react";
var defaultUseHoverStateProps = {
closingDelay: 200,
isDisabled: false
};
var useHoverState = (props = void 0) => {
const { closingDelay, isDisabled } = { ...defaultUseHoverStateProps, ...props };
const [isHovered, setIsHovered] = useState5(false);
const [timer, setTimer] = useState5();
const onMouseEnter = () => {
if (isDisabled) {
return;
}
clearTimeout(timer);
setIsHovered(true);
};
const onMouseLeave = () => {
if (isDisabled) {
return;
}
setTimer(setTimeout(() => {
setIsHovered(false);
}, closingDelay));
};
useEffect5(() => {
if (timer) {
return () => {
clearTimeout(timer);
};
}
});
useEffect5(() => {
if (timer) {
clearTimeout(timer);
}
}, [isDisabled]);
return {
isHovered,
setIsHovered,
handlers: { onMouseEnter, onMouseLeave }
};
};
// src/components/Tooltip.tsx
import { clsx as clsx3 } from "clsx";
import { jsx as jsx5, jsxs as jsxs3 } 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__ */ jsxs3(
"div",
{
className: clsx3("relative inline-block", containerClassName),
...handlers,
children: [
children,
isHovered && /* @__PURE__ */ jsxs3(
"div",
{
className: clsx3(`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__ */ jsx5(
"div",
{
className: clsx3(`absolute w-0 h-0`, triangleClasses[position]),
style: { ...triangleStyle[position], zIndex }
}
)
]
}
)
]
}
);
};
// src/components/modals/ModalRegister.tsx
import { createContext as createContext2, useState as useState6 } from "react";
import { jsx as jsx6 } from "react/jsx-runtime";
var ModalContext = createContext2({
register: [],
addToRegister: noop,
removeFromRegister: noop
});
// src/components/modals/Modal.tsx
import { jsx as jsx7, jsxs as jsxs4 } 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__ */ jsxs4("div", { className: "row justify-between items-start gap-x-8", children: [
title ?? /* @__PURE__ */ jsx7("span", { className: clsx4("textstyle-title-lg", {
"mb-1": description || descriptionText
}), children: titleText }),
!!onCloseClick && /* @__PURE__ */ jsx7(Tooltip, { tooltip: translation.close, children: /* @__PURE__ */ jsx7("button", { className: "row bg-gray-200 hover:bg-gray-300 rounded-md p-1", onClick: onCloseClick, children: /* @__PURE__ */ jsx7(X, {}) }) })
] });
return /* @__PURE__ */ jsxs4("div", { className: "col", children: [
hasTitleRow && titleRow,
description ?? (descriptionText && /* @__PURE__ */ jsx7("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");
}
useEffect6(() => {
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__ */ jsxs4("div", { className: clsx4("fixed inset-0 overflow-y-auto z-[99]", containerClassName), id, children: [
isLast && /* @__PURE__ */ jsx7(
"div",
{
className: clsx4("fixed inset-0 h-screen w-screen", backgroundClassName, {
"bg-black/70": isLast && register.length === 1
}),
onClick: onBackgroundClick
}
),
/* @__PURE__ */ jsxs4(
"div",
{
className: clsx4("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__ */ jsx7(ModalHeader, { ...modalHeaderProps }),
children
]
}
),
!isLast && /* @__PURE__ */ jsx7(
"div",
{
className: clsx4("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 jsx8, jsxs as jsxs5 } 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__ */ jsxs5(Modal, { ...restProps, children: [
children,
/* @__PURE__ */ jsxs5("div", { className: "row mt-3 gap-x-4 justify-end", children: [
onCancel && /* @__PURE__ */ jsx8(
SolidButton,
{
color: buttonOverwrites?.[0].color ?? "primary",
onClick: onCancel,
disabled: buttonOverwrites?.[0].disabled ?? false,
children: buttonOverwrites?.[0].text ?? translation.cancel
}
),
onDecline && /* @__PURE__ */ jsx8(
SolidButton,
{
color: buttonOverwrites?.[1].color ?? "negative",
onClick: onDecline,
disabled: buttonOverwrites?.[1].disabled ?? false,
children: buttonOverwrites?.[1].text ?? translation.decline
}
),
/* @__PURE__ */ jsx8(
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
import { jsx as jsx9 } from "react/jsx-runtime";
var InputModal = ({
inputs,
buttonOverwrites,
...restProps
}) => {
return /* @__PURE__ */ jsx9(
ConfirmDialog,
{
buttonOverwrites,
...restProps,
children: inputs.map((inputProps, index) => /* @__PURE__ */ jsx9(ControlledInput, { ...inputProps }, `input ${index}`))
}
);
};
export {
InputModal
};
//# sourceMappingURL=InputModal.mjs.map