@helpwave/hightide
Version:
helpwave's component and theming library
491 lines (476 loc) • 16.6 kB
JavaScript
// 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 } 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/user-input/Select.tsx
import { Menu } from "@headlessui/react";
import { ChevronDown, ChevronUp } from "lucide-react";
import clsx from "clsx";
// src/components/user-input/Label.tsx
import { jsx as jsx2 } 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__ */ jsx2("label", { ...props, children: children ? children : /* @__PURE__ */ jsx2("span", { className: styleMapping[labelType], children: name }) });
};
// src/components/user-input/Select.tsx
import { Fragment, jsx as jsx3, jsxs } from "react/jsx-runtime";
var Select = ({
value,
label,
options,
onChange,
isHidingCurrentValue = true,
hintText = "",
showDisabledOptions = true,
isDisabled,
className,
textColor = "text-menu-text",
hoverColor = "hover:brightness-90",
additionalItems,
selectedDisplayOverwrite
}) => {
let filteredOptions = isHidingCurrentValue ? options.filter((option) => option.value !== value) : options;
if (!showDisabledOptions) {
filteredOptions = filteredOptions.filter((value2) => !value2.disabled);
}
const selectedOption = options.find((option) => option.value === value);
if (value !== void 0 && selectedOption === void 0 && selectedDisplayOverwrite === void 0) {
console.warn("The selected value is not found in the options list. This might be an error on your part or default behavior if it is complex data type on which === does not work. In case of the latter use selectedDisplayOverwrite to set your selected text or component");
}
const borderColor = "border-menu-border";
return /* @__PURE__ */ jsxs("div", { className: clsx(className), children: [
label && /* @__PURE__ */ jsx3(Label, { ...label, labelType: label.labelType ?? "labelBig", className: clsx("mb-1", label.className) }),
/* @__PURE__ */ jsx3(Menu, { as: "div", className: "relative text-menu-text", children: ({ open }) => /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsxs(
Menu.Button,
{
className: clsx(
"inline-flex w-full justify-between items-center rounded-t-lg border-2 px-4 py-2 font-medium bg-menu-background text-menu-text",
textColor,
borderColor,
{
"rounded-b-lg": !open,
[hoverColor]: !isDisabled,
"bg-disabled-background cursor-not-allowed text-disabled": isDisabled
}
),
disabled: isDisabled,
children: [
/* @__PURE__ */ jsx3("span", { children: selectedDisplayOverwrite ?? selectedOption?.label ?? hintText }),
open ? /* @__PURE__ */ jsx3(ChevronUp, {}) : /* @__PURE__ */ jsx3(ChevronDown, {})
]
}
),
/* @__PURE__ */ jsxs(
Menu.Items,
{
className: "absolute w-full z-10 rounded-b-lg bg-menu-background text-menu-text shadow-lg max-h-[500px] overflow-y-auto",
children: [
(additionalItems ?? []).map((item, index) => /* @__PURE__ */ jsx3(
"div",
{
className: clsx(borderColor, "px-4 py-2 overflow-hidden whitespace-nowrap text-ellipsis border-2 border-t-0", {
"border-b-0 rounded-b-lg": filteredOptions.length === 0 && index === (additionalItems?.length ?? 1) - 1
}),
children: item
},
`additionalItems${index}`
)),
filteredOptions.map((option, index) => /* @__PURE__ */ jsx3(Menu.Item, { children: /* @__PURE__ */ jsx3(
"div",
{
className: clsx(
"px-4 py-2 overflow-hidden whitespace-nowrap text-ellipsis border-2 border-t-0 cursor-pointer",
option.className,
borderColor,
{
"brightness-90": option.value === value,
"brightness-95": index % 2 === 1,
"text-disabled bg-disabled-background cursor-not-allowed": !!option.disabled,
"bg-menu-background text-menu-text hover:brightness-90 cursor-pointer": !option.disabled,
"rounded-b-lg": index === filteredOptions.length - 1
}
),
onClick: () => {
if (!option.disabled) {
onChange(option.value);
}
},
children: option.label
}
) }, `item${index}`))
]
}
)
] }) })
] });
};
// src/components/Button.tsx
import clsx2 from "clsx";
import { jsx as jsx4, 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__ */ jsx4(
"span",
{
className: clsx2({
[iconColorClasses]: !disabled,
[`text-disabled-icon`]: disabled
}),
children: startIcon
}
),
children,
endIcon && /* @__PURE__ */ jsx4(
"span",
{
className: clsx2({
[iconColorClasses]: !disabled,
[`text-disabled-icon`]: disabled
}),
children: endIcon
}
)
]
}
);
};
// 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 clsx4 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 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 useState4 } from "react";
// src/util/noop.ts
var noop = () => void 0;
// src/components/modals/ModalRegister.tsx
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");
}
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__ */ 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/LanguageModal.tsx
import { jsx as jsx8, jsxs as jsxs5 } from "react/jsx-runtime";
var languageDetails = {
en: "English",
de: "Deutsch"
};
var defaultConfirmDialogTranslation = {
en: {
message: "Choose your language",
done: "Done"
},
de: {
message: "W\xE4hlen Sie Ihre Sprache",
done: "Fertig"
}
};
var LanguageModal = ({
overwriteTranslation,
onDone,
onBackgroundClick,
...modalProps
}) => {
const { language, setLanguage } = useLanguage();
const translation = useTranslation(defaultConfirmDialogTranslation, overwriteTranslation);
return /* @__PURE__ */ jsx8(
Modal,
{
titleText: translation.message,
onBackgroundClick: (eventData) => {
onDone();
if (onBackgroundClick) {
onBackgroundClick(eventData);
}
},
...modalProps,
children: /* @__PURE__ */ jsxs5("div", { className: "w-[320px]", children: [
/* @__PURE__ */ jsx8(
Select,
{
className: "mt-2",
value: language,
options: Object.entries(languageDetails).map(([tag, name]) => ({ label: name, value: tag })),
onChange: (language2) => setLanguage(language2)
}
),
/* @__PURE__ */ jsx8("div", { className: "row mt-3 gap-x-4 justify-end", children: /* @__PURE__ */ jsx8(SolidButton, { autoFocus: true, color: "positive", onClick: onDone, children: translation.done }) })
] })
}
);
};
export {
LanguageModal
};
//# sourceMappingURL=LanguageModal.mjs.map