@helpwave/hightide
Version:
helpwave's component and theming library
259 lines (249 loc) • 9.43 kB
JavaScript
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/date/DayPicker.tsx
var DayPicker_exports = {};
__export(DayPicker_exports, {
DayPicker: () => DayPicker,
DayPickerUncontrolled: () => DayPickerUncontrolled
});
module.exports = __toCommonJS(DayPicker_exports);
// src/util/array.ts
var equalSizeGroups = (array, groupSize) => {
if (groupSize <= 0) {
console.warn(`group size should be greater than 0: groupSize = ${groupSize}`);
return [[...array]];
}
const groups = [];
for (let i = 0; i < array.length; i += groupSize) {
groups.push(array.slice(i, Math.min(i + groupSize, array.length)));
}
return groups;
};
// src/util/date.ts
var weekDayList = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
var changeDuration = (date, duration, isAdding) => {
const {
years = 0,
months = 0,
days = 0,
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0
} = duration;
if (years < 0) {
console.error(`Range error years must be greater than 0: received ${years}`);
return new Date(date);
}
if (months < 0 || months > 11) {
console.error(`Range error month must be 0 <= month <= 11: received ${months}`);
return new Date(date);
}
if (days < 0) {
console.error(`Range error days must be greater than 0: received ${days}`);
return new Date(date);
}
if (hours < 0 || hours > 23) {
console.error(`Range error hours must be 0 <= hours <= 23: received ${hours}`);
return new Date(date);
}
if (minutes < 0 || minutes > 59) {
console.error(`Range error minutes must be 0 <= minutes <= 59: received ${minutes}`);
return new Date(date);
}
if (seconds < 0 || seconds > 59) {
console.error(`Range error seconds must be 0 <= seconds <= 59: received ${seconds}`);
return new Date(date);
}
if (milliseconds < 0) {
console.error(`Range error seconds must be greater than 0: received ${milliseconds}`);
return new Date(date);
}
const multiplier = isAdding ? 1 : -1;
const newDate = new Date(date);
newDate.setFullYear(newDate.getFullYear() + multiplier * years);
newDate.setMonth(newDate.getMonth() + multiplier * months);
newDate.setDate(newDate.getDate() + multiplier * days);
newDate.setHours(newDate.getHours() + multiplier * hours);
newDate.setMinutes(newDate.getMinutes() + multiplier * minutes);
newDate.setSeconds(newDate.getSeconds() + multiplier * seconds);
newDate.setMilliseconds(newDate.getMilliseconds() + multiplier * milliseconds);
return newDate;
};
var addDuration = (date, duration) => {
return changeDuration(date, duration, true);
};
var subtractDuration = (date, duration) => {
return changeDuration(date, duration, false);
};
var isInTimeSpan = (value, startDate, endDate) => {
if (startDate && endDate) {
console.assert(startDate <= endDate);
return startDate <= value && value <= endDate;
} else if (startDate) {
return startDate <= value;
} else if (endDate) {
return endDate >= value;
} else {
return true;
}
};
var equalDate = (date1, date2) => {
return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate();
};
var getWeeksForCalenderMonth = (date, weekStart, weeks = 6) => {
const month = date.getMonth();
const year = date.getFullYear();
const dayList = [];
let currentDate = new Date(year, month, 1);
const weekStartIndex = weekDayList.indexOf(weekStart);
while (currentDate.getDay() !== weekStartIndex) {
currentDate = subtractDuration(currentDate, { days: 1 });
}
while (dayList.length < 7 * weeks) {
const date2 = new Date(currentDate);
date2.setHours(date2.getHours(), date2.getMinutes());
dayList.push(date2);
currentDate = addDuration(currentDate, { days: 1 });
}
return equalSizeGroups(dayList, 7);
};
// src/util/noop.ts
var noop = () => void 0;
// src/components/date/DayPicker.tsx
var import_clsx = __toESM(require("clsx"));
// src/localization/LanguageProvider.tsx
var import_react2 = require("react");
// src/hooks/useLocalStorage.ts
var import_react = require("react");
// src/localization/util.ts
var languages = ["en", "de"];
var languagesLocalNames = {
en: "English",
de: "Deutsch"
};
var DEFAULT_LANGUAGE = "en";
var LanguageUtil = {
languages,
DEFAULT_LANGUAGE,
languagesLocalNames
};
// src/localization/LanguageProvider.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var LanguageContext = (0, import_react2.createContext)({
language: LanguageUtil.DEFAULT_LANGUAGE,
setLanguage: (v) => v
});
var useLanguage = () => (0, import_react2.useContext)(LanguageContext);
var useLocale = (overWriteLanguage) => {
const { language } = useLanguage();
const mapping = {
en: "en-US",
de: "de-DE"
};
return mapping[overWriteLanguage ?? language];
};
// src/components/date/DayPicker.tsx
var import_react3 = require("react");
var import_jsx_runtime2 = require("react/jsx-runtime");
var DayPicker = ({
displayedMonth,
selected,
start,
end,
onChange = noop,
weekStart = "monday",
markToday = true,
className = ""
}) => {
const locale = useLocale();
const month = displayedMonth.getMonth();
const weeks = getWeeksForCalenderMonth(displayedMonth, weekStart);
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: (0, import_clsx.default)("flex-col-1 min-w-[220px] select-none", className), children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "flex-row-2 text-center", children: weeks[0].map((weekDay, index) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "flex-1 font-semibold", children: new Intl.DateTimeFormat(locale, { weekday: "long" }).format(weekDay).substring(0, 2) }, index)) }),
weeks.map((week, index) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "flex-row-2 text-center", children: week.map((date) => {
const isSelected = !!selected && equalDate(selected, date);
const isToday = equalDate(/* @__PURE__ */ new Date(), date);
const isSameMonth = date.getMonth() === month;
const isDayValid = isInTimeSpan(date, start, end);
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
"button",
{
disabled: !isDayValid,
className: (0, import_clsx.default)(
"flex-1 rounded-full border-2",
{
"text-description": !isSameMonth && !isSelected && isDayValid,
"text-button-solid-neutral-text bg-button-solid-neutral-background": !isSelected && isSameMonth && isDayValid,
"text-button-solid-primary-text bg-button-solid-primary-background": isSelected && isDayValid,
"hover:brightness-90 hover:bg-button-solid-primary-background hover:text-button-solid-primary-text": isDayValid,
"text-disabled-text bg-disabled-background cursor-not-allowed": !isDayValid,
"border-secondary": isToday && markToday,
"border-transparent": !isToday || !markToday
}
),
onClick: () => onChange(date),
children: date.getDate()
},
date.getDate()
);
}) }, index))
] });
};
var DayPickerUncontrolled = ({
displayedMonth,
selected,
onChange = noop,
...restProps
}) => {
const [date, setDate] = (0, import_react3.useState)(selected);
const [usedDisplayedMonth, setUsedDDisplayedMonth] = (0, import_react3.useState)(displayedMonth);
(0, import_react3.useEffect)(() => {
setDate(selected);
setUsedDDisplayedMonth(selected);
}, [selected]);
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
DayPicker,
{
displayedMonth: usedDisplayedMonth,
selected: date,
onChange: (newDate) => {
setDate(newDate);
setUsedDDisplayedMonth(newDate);
onChange(newDate);
},
...restProps
}
);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
DayPicker,
DayPickerUncontrolled
});
//# sourceMappingURL=DayPicker.js.map