UNPKG

@renderlesskit/react

Version:

Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit

137 lines (119 loc) 6.13 kB
var _excluded = ["onFocus", "onClick", "ref"]; function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * All credit goes to [React Spectrum](https://github.com/adobe/react-spectrum) * We improved the Calendar from Aria [useCalendarBase](https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/calendar/src/useCalendarBase.ts) * to work with Reakit System */ import * as React from "react"; import { useButton } from "reakit"; import { ensureFocus, useForkRef } from "reakit-utils"; import { callAllHandlers } from "@chakra-ui/utils"; import { useDateFormatter } from "@react-aria/i18n"; import { createComponent, createHook } from "../system"; import { isSameDay } from "../utils"; import { CALENDAR_CELL_BUTTON_KEYS } from "./__keys"; export var useCalendarCellButton = createHook({ name: "CalendarCellButton", compose: useButton, keys: CALENDAR_CELL_BUTTON_KEYS, useOptions(options, _ref) { var { disabled } = _ref; var { isDisabled: isDisabledOption, date, month, isInvalidDateRange } = options; var isCurrentMonth = date.getMonth() === month; var isDisabled = isDisabledOption || !isCurrentMonth || isInvalidDateRange(date); var truelyDisabled = disabled || isDisabled; return _objectSpread({ disabled: truelyDisabled }, options); }, useProps(options, _ref2) { var { onFocus: htmlOnFocus, onClick: htmlOnClick, ref: htmlRef } = _ref2, htmlProps = _objectWithoutProperties(_ref2, _excluded); var { date, disabled, dateValue, selectDate, anchorDate, focusedDate, isDisabled, setFocusedDate, isFocused: isFocusedOption } = options; var ref = React.useRef(null); var isSelected = dateValue ? isSameDay(date, dateValue) : false; var isFocused = isFocusedOption && focusedDate && isSameDay(date, focusedDate); var isToday = isSameDay(date, new Date()); // Focus the button in the DOM when the state updates. React.useEffect(() => { if (isFocused && ref.current) { ensureFocus(ref.current); } }, [date, focusedDate, isFocused, ref]); var onClick = React.useCallback(() => { if (disabled) return; selectDate(date); setFocusedDate(date); }, [date, disabled, selectDate, setFocusedDate]); var onFocus = React.useCallback(() => { if (disabled) return; setFocusedDate(date); }, [date, disabled, setFocusedDate]); var dateFormatter = useDateFormatter({ weekday: "long", day: "numeric", month: "long", year: "numeric" }); // aria-label should be localize Day of week, Month, Day and Year without Time. function getAriaLabel() { var ariaLabel = dateFormatter.format(date); var isTodayLabel = isToday ? "Today, " : ""; var isSelctedLabel = isSelected ? " selected" : ""; ariaLabel = "".concat(isTodayLabel).concat(ariaLabel).concat(isSelctedLabel); // When a cell is focused and this is a range calendar, add a prompt to help // screenreader users know that they are in a range selection mode. if (options.isRangeCalendar && isFocused && !isDisabled) { var rangeSelectionPrompt = ""; // If selection has started add "click to finish selecting range" if (anchorDate) { rangeSelectionPrompt = "click to finish selecting range"; // Otherwise, add "click to start selecting range" prompt } else { rangeSelectionPrompt = "click to start selecting range"; } // Append to aria-label if (rangeSelectionPrompt) { ariaLabel = "".concat(ariaLabel, " (").concat(rangeSelectionPrompt, ")"); } } return ariaLabel; } return _objectSpread({ children: useDateFormatter({ day: "numeric" }).format(date), "aria-label": getAriaLabel(), tabIndex: !disabled ? isSameDay(date, focusedDate) ? 0 : -1 : undefined, ref: useForkRef(ref, htmlRef), onClick: callAllHandlers(htmlOnClick, onClick), onFocus: callAllHandlers(htmlOnFocus, onFocus) }, htmlProps); } }); export var CalendarCellButton = createComponent({ as: "span", memo: true, useHook: useCalendarCellButton }); //# sourceMappingURL=CalendarCellButton.js.map