@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
51 lines • 2.33 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { addMonths, addYears, format, isSameDay, subMonths, subYears, } from "date-fns";
import React, { forwardRef } from "react";
import { useRenameCSS } from "../../theme/Theme.js";
import { Detail } from "../../typography/Detail.js";
import { useI18n } from "../../util/i18n/i18n.hooks.js";
import { useTimelineContext } from "../hooks/useTimelineContext.js";
export const ZoomButton = forwardRef((_a, ref) => {
var { label, interval, count } = _a, rest = __rest(_a, ["label", "interval", "count"]);
const { setStart, endDate, startDate, direction } = useTimelineContext();
const translate = useI18n("Timeline");
const dateFormat = translate("dateFormat");
const { cn } = useRenameCSS();
let startOfRange;
if (interval === "month") {
startOfRange =
direction === "left"
? subMonths(endDate, count)
: addMonths(startDate, count);
}
else {
startOfRange =
direction === "left"
? subYears(endDate, count)
: addYears(startDate, count);
}
const currentZoom = direction === "left"
? isSameDay(startDate, startOfRange)
: isSameDay(endDate, startOfRange);
return (React.createElement(Detail, { as: "li" },
React.createElement("button", Object.assign({ type: "button", "aria-label": !currentZoom
? translate("Zoom.zoom", {
start: format(startOfRange, dateFormat),
end: format(endDate, dateFormat),
})
: translate("Zoom.reset"), ref: ref }, rest, { className: cn("navds-timeline__zoom-button"), onClick: () => {
setStart(startOfRange);
}, "aria-pressed": currentZoom }), label)));
});
export default ZoomButton;
//# sourceMappingURL=ZoomButton.js.map