UNPKG

@wordpress/components

Version:
330 lines (329 loc) • 17.7 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js 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 __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let date_fns = require("date-fns"); let clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_icons = require("@wordpress/icons"); let _wordpress_date = require("@wordpress/date"); let _wordpress_element = require("@wordpress/element"); let __use_lilius_index_ts = require("./use-lilius/index.cjs"); let ______button_index_tsx = require("../../button/index.cjs"); ______button_index_tsx = __toESM(______button_index_tsx); let ______h_stack_index_ts = require("../../h-stack/index.cjs"); let ______heading_index_ts = require("../../heading/index.cjs"); let ___utils_ts = require("../utils.cjs"); let ___constants_ts = require("../constants.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/style-runtime/src/index.ts const STYLE_HASH_ATTRIBUTE = "data-wp-hash"; /** * Returns the shared style runtime registry. * * The registry is stored on `globalThis` so separately bundled copies of this * package can coordinate through the same document and style maps. * * @return The shared runtime registry. */ function getRuntime() { const globalScope = globalThis; if (globalScope.__wpStyleRuntime) return globalScope.__wpStyleRuntime; globalScope.__wpStyleRuntime = { documents: /* @__PURE__ */ new Map(), styles: /* @__PURE__ */ new Map(), injectedStyles: /* @__PURE__ */ new WeakMap() }; if (typeof document !== "undefined") registerDocument(document); return globalScope.__wpStyleRuntime; } /** * Checks whether a document already contains a style tag for a hash. * * @param targetDocument Document to inspect. * @param hash Stable hash for the transformed CSS. * * @return Whether the style hash already exists in the document. */ function documentContainsStyleHash(targetDocument, hash) { if (!targetDocument.head) return false; for (const style of targetDocument.head.querySelectorAll(`style[${STYLE_HASH_ATTRIBUTE}]`)) if (style.getAttribute(STYLE_HASH_ATTRIBUTE) === hash) return true; return false; } /** * Injects a registered style into a document, unless that document already * contains a style tag for the same hash. * * @param targetDocument Document to inject the style into. * @param hash Stable hash for the transformed CSS. * @param css CSS text to inject. */ function injectStyle(targetDocument, hash, css) { if (!targetDocument.head) return; const runtime = getRuntime(); let injectedStyles = runtime.injectedStyles.get(targetDocument); if (!injectedStyles) { injectedStyles = /* @__PURE__ */ new Set(); runtime.injectedStyles.set(targetDocument, injectedStyles); } if (injectedStyles.has(hash)) return; if (documentContainsStyleHash(targetDocument, hash)) { injectedStyles.add(hash); return; } const style = targetDocument.createElement("style"); style.setAttribute(STYLE_HASH_ATTRIBUTE, hash); style.appendChild(targetDocument.createTextNode(css)); targetDocument.head.appendChild(style); injectedStyles.add(hash); } /** * Registers a document as a style injection target. * * Existing registered styles are replayed into the document immediately. * Documents are reference-counted so multiple providers can safely register the * same document without one cleanup removing it while another registration is * still active. * * @param targetDocument Document to receive registered styles. * @return Cleanup function that unregisters this document registration. */ function registerDocument(targetDocument) { const runtime = getRuntime(); runtime.documents.set(targetDocument, (runtime.documents.get(targetDocument) ?? 0) + 1); for (const [hash, css] of runtime.styles) injectStyle(targetDocument, hash, css); return () => { const count = runtime.documents.get(targetDocument); if (count === void 0) return; if (count <= 1) { runtime.documents.delete(targetDocument); return; } runtime.documents.set(targetDocument, count - 1); }; } /** * Registers a style and injects it into all registered documents. * * The hash is used as the deduplication key, so calling this repeatedly with * the same hash will not add duplicate style tags to a document. * Registered styles are retained for the lifetime of the page so they can be * replayed into documents that are registered later. * * @param hash Stable hash for the transformed CSS. * @param css CSS text to inject. */ function registerStyle(hash, css) { const runtime = getRuntime(); runtime.styles.set(hash, css); for (const targetDocument of runtime.documents.keys()) injectStyle(targetDocument, hash, css); } // packages/components/src/date-time/date-picker/style.module.scss if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("9ccda9ca28", "._4133ce7f8f5171a6__wrapper{box-sizing:border-box}._4133ce7f8f5171a6__wrapper *,._4133ce7f8f5171a6__wrapper :after,._4133ce7f8f5171a6__wrapper :before{box-sizing:inherit}._5d71bd33c79ccd8a__navigator{grid-template-columns:.5fr repeat(5,1fr) .5fr;justify-items:center;margin-bottom:var(--wpds-dimension-gap-lg,16px)}._5d71bd33c79ccd8a__navigator._5d71bd33c79ccd8a__navigator{display:grid}._84049a4038b5abaa__previous-month{grid-column:1/2}._2ed0fb8188d62fd2__next-month{grid-column:7/8}._640229ee401af16b__heading{grid-column:2/7}._640229ee401af16b__heading strong{font-weight:var(--wpds-typography-font-weight-emphasis,600)}._640229ee401af16b__heading._640229ee401af16b__heading{font-size:var(--wpds-typography-font-size-md,13px);font-weight:var(--wpds-typography-font-weight-default,400)}.a6fafdc57e4ec7ef__calendar{column-gap:var(--wpds-dimension-gap-sm,8px);display:grid;grid-template-columns:.5fr repeat(5,1fr) .5fr;justify-items:center;row-gap:var(--wpds-dimension-gap-sm,8px)}._8ea229147ce1d280__day-of-week{color:var(--wpds-color-foreground-content-neutral-weak,#6e6e6e);font-size:var(--wpds-typography-font-size-md,13px);line-height:1.4}._15b622d97c3ea916__day{grid-column:var(--_wp-components-date-picker-column);justify-content:center;padding:0;position:relative}._15b622d97c3ea916__day:where(:disabled){pointer-events:none}._15b622d97c3ea916__day:where(.f4c4764286fd7b88__has-events):before{border:var(--wpds-border-width-sm,2px) solid var(--wpds-color-stroke-interactive-brand,var(--wp-admin-theme-color,#3858e9));border-radius:50%;content:\" \";left:50%;position:absolute;transform:translate(-50%,9px)}._15b622d97c3ea916__day:where(.f4c4764286fd7b88__has-events.ddd970c7f061156e__is-selected):before{border-color:var(--wpds-color-foreground-interactive-brand-strong,#fff)}._15b622d97c3ea916__day._15b622d97c3ea916__day._15b622d97c3ea916__day{border-radius:50%;font-size:var(--wpds-typography-font-size-md,13px);font-weight:var(--wpds-typography-font-weight-default,400);height:28px;width:28px}._15b622d97c3ea916__day._15b622d97c3ea916__day._15b622d97c3ea916__day:where(.ddd970c7f061156e__is-selected){background:var(--wpds-color-background-interactive-brand-strong,var(--wp-admin-theme-color,#3858e9))}._15b622d97c3ea916__day._15b622d97c3ea916__day._15b622d97c3ea916__day:where(.ddd970c7f061156e__is-selected),._15b622d97c3ea916__day._15b622d97c3ea916__day._15b622d97c3ea916__day:where(.ddd970c7f061156e__is-selected):hover:not(:disabled,[aria-disabled=true]){color:var(--wpds-color-foreground-interactive-brand-strong,#fff)}._15b622d97c3ea916__day._15b622d97c3ea916__day._15b622d97c3ea916__day:where(.ddd970c7f061156e__is-selected):focus:not(:disabled){border:var(--wpds-border-width-focus,var(--wp-admin-border-width-focus,2px)) solid}._15b622d97c3ea916__day._15b622d97c3ea916__day._15b622d97c3ea916__day:where(.ddd970c7f061156e__is-selected):after{border:var(--wpds-border-width-xs,1px) solid transparent;border-radius:inherit;content:\"\";inset:0;pointer-events:none;position:absolute}._15b622d97c3ea916__day._15b622d97c3ea916__day._15b622d97c3ea916__day:where(._9af06b51a4cb7f3d__is-today){background:var(--wpds-color-background-surface-neutral-weak,#f3f3f3)}"); var style_module_default = { "wrapper": "_4133ce7f8f5171a6__wrapper", "navigator": "_5d71bd33c79ccd8a__navigator", "previous-month": "_84049a4038b5abaa__previous-month", "next-month": "_2ed0fb8188d62fd2__next-month", "heading": "_640229ee401af16b__heading", "calendar": "a6fafdc57e4ec7ef__calendar", "day-of-week": "_8ea229147ce1d280__day-of-week", "day": "_15b622d97c3ea916__day", "has-events": "f4c4764286fd7b88__has-events", "is-selected": "ddd970c7f061156e__is-selected", "is-today": "_9af06b51a4cb7f3d__is-today" }; // packages/components/src/date-time/date-picker/index.tsx /** * DatePicker is a React component that renders a calendar for date selection. * * ```jsx * import { DatePicker } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * const MyDatePicker = () => { * const [ date, setDate ] = useState( new Date() ); * * return ( * <DatePicker * currentDate={ date } * onChange={ ( newDate ) => setDate( newDate ) } * /> * ); * }; * ``` */ function DatePicker({ currentDate, onChange, events = [], isInvalidDate, onMonthPreviewed, startOfWeek: weekStartsOn = 0 }) { const date = (0, ___utils_ts.inputToDate)(currentDate ?? /* @__PURE__ */ new Date()); const { calendar, viewing, setSelected, setViewing, isSelected, viewPreviousMonth, viewNextMonth } = (0, __use_lilius_index_ts.useLilius)({ selected: [(0, ___utils_ts.startOfDayInConfiguredTimezone)(date)], viewing: (0, ___utils_ts.startOfDayInConfiguredTimezone)(date), weekStartsOn }); const [focusable, setFocusable] = (0, _wordpress_element.useState)((0, ___utils_ts.startOfDayInConfiguredTimezone)(date)); const [isFocusWithinCalendar, setIsFocusWithinCalendar] = (0, _wordpress_element.useState)(false); const [prevCurrentDate, setPrevCurrentDate] = (0, _wordpress_element.useState)(currentDate); if (currentDate !== prevCurrentDate) { setPrevCurrentDate(currentDate); setSelected([(0, ___utils_ts.startOfDayInConfiguredTimezone)(date)]); setViewing((0, ___utils_ts.startOfDayInConfiguredTimezone)(date)); setFocusable((0, ___utils_ts.startOfDayInConfiguredTimezone)(date)); } return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className: (0, clsx.default)("components-datetime__date", style_module_default.wrapper), role: "application", "aria-label": (0, _wordpress_i18n.__)("Calendar"), children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(______h_stack_index_ts.HStack, { className: style_module_default.navigator, children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______button_index_tsx.default, { className: style_module_default["previous-month"], icon: (0, _wordpress_i18n.isRTL)() ? _wordpress_icons.arrowRight : _wordpress_icons.arrowLeft, variant: "tertiary", "aria-label": (0, _wordpress_i18n.__)("View previous month"), onClick: () => { viewPreviousMonth(); setFocusable((0, date_fns.subMonths)(focusable, 1)); const prevMonth = (0, date_fns.subMonths)(viewing, 1); onMonthPreviewed?.((0, _wordpress_date.dateI18n)(___constants_ts.TIMEZONELESS_FORMAT, prevMonth, -prevMonth.getTimezoneOffset())); }, size: "compact" }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(______heading_index_ts.Heading, { level: 3, className: style_module_default.heading, children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("strong", { children: (0, _wordpress_date.dateI18n)("F", viewing, -viewing.getTimezoneOffset()) }), " ", (0, _wordpress_date.dateI18n)("Y", viewing, -viewing.getTimezoneOffset()) ] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______button_index_tsx.default, { className: style_module_default["next-month"], icon: (0, _wordpress_i18n.isRTL)() ? _wordpress_icons.arrowLeft : _wordpress_icons.arrowRight, variant: "tertiary", "aria-label": (0, _wordpress_i18n.__)("View next month"), onClick: () => { viewNextMonth(); setFocusable((0, date_fns.addMonths)(focusable, 1)); const nextMonth = (0, date_fns.addMonths)(viewing, 1); onMonthPreviewed?.((0, _wordpress_date.dateI18n)(___constants_ts.TIMEZONELESS_FORMAT, nextMonth, -nextMonth.getTimezoneOffset())); }, size: "compact" }) ] }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className: style_module_default.calendar, onFocus: () => setIsFocusWithinCalendar(true), onBlur: () => setIsFocusWithinCalendar(false), children: [calendar[0][0].map((day) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: style_module_default["day-of-week"], children: (0, _wordpress_date.dateI18n)("D", day, -day.getTimezoneOffset()) }, day.toString())), calendar[0].map((week) => week.map((day, index) => { if (!(0, date_fns.isSameMonth)(day, viewing)) return null; return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Day, { day, column: index + 1, isSelected: isSelected(day), isFocusable: (0, date_fns.isEqual)(day, focusable), isFocusAllowed: isFocusWithinCalendar, isToday: (0, date_fns.isSameDay)(day, (0, ___utils_ts.startOfDayInConfiguredTimezone)(/* @__PURE__ */ new Date())), isInvalid: isInvalidDate ? isInvalidDate(day) : false, numEvents: events.filter((event) => (0, date_fns.isSameDay)(event.date, day)).length, onClick: () => { setSelected([day]); setFocusable(day); const newDate = (0, ___utils_ts.setInConfiguredTimezone)(date, { year: day.getFullYear(), month: day.getMonth(), date: day.getDate() }); onChange?.((0, _wordpress_date.date)(___constants_ts.TIMEZONELESS_FORMAT, newDate)); }, onKeyDown: (event) => { let nextFocusable; if (event.key === "ArrowLeft") nextFocusable = (0, date_fns.addDays)(day, (0, _wordpress_i18n.isRTL)() ? 1 : -1); if (event.key === "ArrowRight") nextFocusable = (0, date_fns.addDays)(day, (0, _wordpress_i18n.isRTL)() ? -1 : 1); if (event.key === "ArrowUp") nextFocusable = (0, date_fns.subWeeks)(day, 1); if (event.key === "ArrowDown") nextFocusable = (0, date_fns.addWeeks)(day, 1); if (event.key === "PageUp") nextFocusable = (0, date_fns.subMonths)(day, 1); if (event.key === "PageDown") nextFocusable = (0, date_fns.addMonths)(day, 1); if (event.key === "Home") { const daysToSubtract = (day.getDay() - weekStartsOn + 7) % 7; nextFocusable = (0, date_fns.subDays)(day, daysToSubtract); } if (event.key === "End") { const dayOfWeek = day.getDay(); const daysToAdd = (weekStartsOn + 6 - dayOfWeek) % 7; nextFocusable = (0, date_fns.addDays)(day, daysToAdd); } if (nextFocusable) { event.preventDefault(); setFocusable(nextFocusable); if (!(0, date_fns.isSameMonth)(nextFocusable, viewing)) { setViewing(nextFocusable); onMonthPreviewed?.((0, _wordpress_date.dateI18n)(___constants_ts.TIMEZONELESS_FORMAT, nextFocusable, -nextFocusable.getTimezoneOffset())); } } } }, day.toString()); }))] })] }); } function Day({ day, column, isSelected, isFocusable, isFocusAllowed, isToday, isInvalid, numEvents, onClick, onKeyDown }) { const ref = (0, _wordpress_element.useRef)(null); (0, _wordpress_element.useEffect)(() => { if (ref.current && isFocusable && isFocusAllowed) ref.current.focus(); }, [isFocusable]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______button_index_tsx.default, { __next40pxDefaultSize: true, ref, className: (0, clsx.default)("components-datetime__date__day", style_module_default.day, { [style_module_default["is-selected"]]: isSelected, [style_module_default["is-today"]]: !isSelected && isToday, [style_module_default["has-events"]]: numEvents > 0 }), style: { "--_wp-components-date-picker-column": column }, disabled: isInvalid, tabIndex: isFocusable ? 0 : -1, "aria-label": getDayLabel(day, isSelected, isToday, numEvents), onClick, onKeyDown, children: (0, _wordpress_date.dateI18n)("j", day, -day.getTimezoneOffset()) }); } function getDayLabel(date, isSelected, isToday, numEvents) { const { formats } = (0, _wordpress_date.getSettings)(); const parts = [(0, _wordpress_date.dateI18n)(formats.date, date, -date.getTimezoneOffset())]; if (isSelected) parts.push((0, _wordpress_i18n.__)("Selected")); if (isToday) parts.push((0, _wordpress_i18n.__)("Today")); if (numEvents > 0) parts.push((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n._n)("There is %d event", "There are %d events", numEvents), numEvents)); return parts.join(". "); } exports.DatePicker = DatePicker; exports.default = DatePicker; //# sourceMappingURL=index.cjs.map