@helpwave/hightide
Version:
helpwave's component and theming library
291 lines (279 loc) • 8.73 kB
JavaScript
// src/components/properties/CheckboxProperty.tsx
import { Check as Check2 } from "lucide-react";
// src/util/noop.ts
var noop = () => void 0;
// src/components/user-input/Checkbox.tsx
import { useState } from "react";
import * as CheckboxPrimitive from "@radix-ui/react-checkbox";
import { Check, Minus } from "lucide-react";
import clsx from "clsx";
// 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/Checkbox.tsx
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
var checkboxSizeMapping = {
small: "size-4",
medium: "size-6",
large: "size-8"
};
var checkboxIconSizeMapping = {
small: "size-3",
medium: "size-5",
large: "size-7"
};
var ControlledCheckbox = ({
id,
label,
checked,
disabled,
onChange,
onChangeTristate,
size = "medium",
className = "",
containerClassName
}) => {
const usedSizeClass = checkboxSizeMapping[size];
const innerIconSize = checkboxIconSizeMapping[size];
const propagateChange = (checked2) => {
if (onChangeTristate) {
onChangeTristate(checked2);
}
if (onChange) {
onChange(checked2 === "indeterminate" ? false : checked2);
}
};
const changeValue = () => {
const newValue = checked === "indeterminate" ? false : !checked;
propagateChange(newValue);
};
return /* @__PURE__ */ jsxs("div", { className: clsx("row justify-center items-center", containerClassName), children: [
/* @__PURE__ */ jsx2(
CheckboxPrimitive.Root,
{
onCheckedChange: propagateChange,
checked,
disabled,
id,
className: clsx(usedSizeClass, `items-center border-2 rounded outline-none focus:border-primary`, {
"text-disabled-text border-disabled-text": disabled,
"border-on-background": !disabled,
"bg-primary/30 border-primary text-primary": checked === true || checked === "indeterminate",
"hover:border-gray-400 focus:hover:border-primary": !checked
}, className),
children: /* @__PURE__ */ jsxs(CheckboxPrimitive.Indicator, { children: [
checked === true && /* @__PURE__ */ jsx2(Check, { className: innerIconSize }),
checked === "indeterminate" && /* @__PURE__ */ jsx2(Minus, { className: innerIconSize })
] })
}
),
label && /* @__PURE__ */ jsx2(Label, { ...label, className: clsx("cursor-pointer", label.className), htmlFor: id, onClick: changeValue })
] });
};
// src/hooks/useLanguage.tsx
import { createContext, useContext, useEffect as useEffect2, useState as useState3 } from "react";
// src/hooks/useLocalStorage.tsx
import { useCallback, useEffect, useState as useState2 } from "react";
// src/hooks/useLanguage.tsx
import { jsx as jsx3 } 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/properties/PropertyBase.tsx
import { AlertTriangle } from "lucide-react";
import clsx3 from "clsx";
// 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 TextButton = ({
children,
disabled = false,
color = "neutral",
size = "medium",
startIcon,
endIcon,
onClick,
className,
...restProps
}) => {
const colorClasses = {
negative: "bg-transparent text-button-text-negative-text",
neutral: "bg-transparent text-button-text-neutral-text"
}[color];
const iconColorClasses = {
negative: "text-button-text-negative-icon",
neutral: "text-button-text-neutral-icon"
}[color];
return /* @__PURE__ */ jsxs2(
"button",
{
onClick: disabled ? void 0 : onClick,
disabled: disabled || onClick === void 0,
className: clsx2(
className,
{
"text-disabled-text": disabled,
[clsx2(colorClasses, "hover:bg-button-text-hover-background rounded-full")]: !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/properties/PropertyBase.tsx
import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
var defaultPropertyBaseTranslation = {
en: {
remove: "Remove"
},
de: {
remove: "Entfernen"
}
};
var PropertyBase = ({
overwriteTranslation,
name,
input,
softRequired = false,
hasValue,
icon,
readOnly,
onRemove,
className = ""
}) => {
const translation = useTranslation(defaultPropertyBaseTranslation, overwriteTranslation);
const requiredAndNoValue = softRequired && !hasValue;
return /* @__PURE__ */ jsxs3("div", { className: clsx3("row gap-x-0 group", className), children: [
/* @__PURE__ */ jsxs3(
"div",
{
className: clsx3("row gap-x-2 !w-[200px] px-4 py-2 items-center rounded-l-xl border-2 border-r-0", {
"bg-gray-100 text-black group-hover:border-primary border-gray-400": !requiredAndNoValue,
"bg-warning text-surface-warning group-hover:border-warning border-warning/90": requiredAndNoValue
}, className),
children: [
icon,
name
]
}
),
/* @__PURE__ */ jsxs3(
"div",
{
className: clsx3("row grow justify-between items-center rounded-r-xl border-2 border-l-0", {
"bg-white group-hover:border-primary border-gray-400": !requiredAndNoValue,
"bg-surface-warning group-hover:border-warning border-warning/90": requiredAndNoValue
}, className),
children: [
input({ softRequired, hasValue }),
requiredAndNoValue && /* @__PURE__ */ jsx5("div", { className: "text-warning pr-4", children: /* @__PURE__ */ jsx5(AlertTriangle, { size: 24 }) }),
onRemove && /* @__PURE__ */ jsx5(
TextButton,
{
onClick: onRemove,
color: "negative",
className: clsx3("pr-4 items-center", { "!text-transparent": !hasValue || readOnly }),
disabled: !hasValue || readOnly,
children: translation.remove
}
)
]
}
)
] });
};
// src/components/properties/CheckboxProperty.tsx
import { jsx as jsx6 } from "react/jsx-runtime";
var defaultCheckboxPropertyTranslation = {
en: {
yes: "Yes",
no: "No"
},
de: {
yes: "Ja",
no: "Nein"
}
};
var CheckboxProperty = ({
overwriteTranslation,
value,
onChange = noop,
readOnly,
...baseProps
}) => {
const translation = useTranslation(defaultCheckboxPropertyTranslation, overwriteTranslation);
return /* @__PURE__ */ jsx6(
PropertyBase,
{
...baseProps,
hasValue: true,
readOnly,
icon: /* @__PURE__ */ jsx6(Check2, { size: 16 }),
input: () => /* @__PURE__ */ jsx6("div", { className: "row py-2 px-4 items-center", children: /* @__PURE__ */ jsx6(
ControlledCheckbox,
{
checked: value ?? true,
disabled: readOnly,
onChange,
label: { name: `${translation.yes}/${translation.no}`, labelType: "labelMedium" }
}
) })
}
);
};
export {
CheckboxProperty
};
//# sourceMappingURL=CheckboxProperty.mjs.map