Skip to content

Commit

Permalink
fix: add ChevronFlag for disabled chevrons (#2178)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpbl authored Jun 1, 2024
1 parent 549da00 commit b06d537
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 15 deletions.
4 changes: 1 addition & 3 deletions examples/HideNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ import React from "react";
import { DayPicker } from "react-day-picker";

export function HideNavigation() {
return (
<DayPicker hideNavigation disableNavigation captionLayout="dropdown" />
);
return <DayPicker hideNavigation captionLayout="dropdown" />;
}
6 changes: 6 additions & 0 deletions src/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ export enum CalendarFlag {
/** Assigned when the calendar has multiple months. */
hasMultipleMonths = "has_multiple_months"
}

/** Flags that can be applied to the {@link UI.Chevron} element. */
export enum ChevronFlag {
/** Assigned when the week numbers are show. */
disabled = "chevron_disabled"
}
12 changes: 9 additions & 3 deletions src/components/Chevron.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

import { UI } from "../UI";
import { ChevronFlag, UI } from "../UI";
import { useProps } from "../contexts/props";

/**
Expand All @@ -16,11 +16,17 @@ export function Chevron(props: {
orientation?: "up" | "down" | "left" | "right";
}) {
const { size = 24, orientation = "left" } = props;
const { classNames } = useProps();
const { classNames, disableNavigation } = useProps();

const svgClassName = [
classNames[UI.Chevron],
disableNavigation ? classNames[ChevronFlag.disabled] : ""
]
.join(" ")
.trim();
return (
<svg
className={classNames[UI.Chevron]}
className={svgClassName}
width={size}
height={size}
viewBox="0 0 24 24"
Expand Down
3 changes: 1 addition & 2 deletions src/components/MonthsDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ export function MonthsDropdown(props: {

return (
<Dropdown
name="month"
aria-label={labelMonthDropdown()}
aria-disabled={disableNavigation ? "true" : undefined}
disabled={Boolean(disableNavigation)}
rootClassName={classNames[UI.MonthsDropdown]}
options={dropdown.months}
value={props.month.date.getMonth()}
Expand Down
2 changes: 1 addition & 1 deletion src/components/YearsDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function YearsDropdown(props: {
return (
<Dropdown
aria-label={labelYearDropdown()}
aria-disabled={disableNavigation ? "true" : undefined}
disabled={Boolean(disableNavigation)}
rootClassName={classNames[UI.YearsDropdown]}
options={dropdown.years}
value={props.month.date.getFullYear()}
Expand Down
1 change: 1 addition & 0 deletions src/helpers/getDefaultClassNames.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test("should return the default classnames", () => {
calendar: "rdp-calendar",
caption_label: "rdp-caption_label",
chevron: "rdp-chevron",
chevron_disabled: "rdp-chevron_disabled",
day: "rdp-day",
disabled: "rdp-disabled",
dropdown_nav: "rdp-dropdown_nav",
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/getDefaultClassNames.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UI, DayModifier, CalendarFlag } from "../UI";
import { UI, DayModifier, CalendarFlag, ChevronFlag } from "../UI";
import type { ClassNames } from "../types";

/**
Expand All @@ -19,6 +19,11 @@ export function getDefaultClassNames(): Required<ClassNames> {
`rdp-${CalendarFlag[key as keyof typeof CalendarFlag]}`;
}

for (const key in ChevronFlag) {
classNames[ChevronFlag[key as keyof typeof ChevronFlag]] =
`rdp-${ChevronFlag[key as keyof typeof ChevronFlag]}`;
}

for (const key in DayModifier) {
classNames[DayModifier[key as keyof typeof DayModifier]] =
`rdp-${DayModifier[key as keyof typeof DayModifier]}`;
Expand Down
9 changes: 7 additions & 2 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@

/* The gap between the dropdown. */
--rdp-dropdown-gap: 0.5rem;

/* The gap between the dropdown. */
--rdp-chevron-disabled-opacity: 0.5;
}

.rdp-button_next,
Expand Down Expand Up @@ -162,6 +165,10 @@
fill: var(--rdp-accent-color);
}

.rdp-chevron_disabled {
opacity: var(--rdp-chevron-disabled-opacity);
}

.rdp-calendar[dir="rtl"] .rdp-chevron {
transform: rotate(180deg);
}
Expand Down Expand Up @@ -197,8 +204,6 @@
}

.rdp-dropdown[disabled] {
opacity: unset;
color: unset;
}

.rdp-dropdown_root {
Expand Down
1 change: 1 addition & 0 deletions src/style.css.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare const styles: {
readonly "rdp-calendar": string;
readonly "rdp-caption_label": string;
readonly "rdp-chevron": string;
readonly "rdp-chevron_disabled": string;
readonly "rdp-day": string;
readonly "rdp-disabled": string;
readonly "rdp-dropdown": string;
Expand Down
9 changes: 7 additions & 2 deletions src/style.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@

/* The gap between the dropdown. */
--rdp-dropdown-gap: 0.5rem;

/* The gap between the dropdown. */
--rdp-chevron-disabled-opacity: 0.5;
}

.button_next,
Expand Down Expand Up @@ -162,6 +165,10 @@
fill: var(--rdp-accent-color);
}

.chevron_disabled {
opacity: var(--rdp-chevron-disabled-opacity);
}

.calendar[dir="rtl"] .chevron {
transform: rotate(180deg);
}
Expand Down Expand Up @@ -197,8 +204,6 @@
}

.dropdown[disabled] {
opacity: unset;
color: unset;
}

.dropdown_root {
Expand Down
1 change: 1 addition & 0 deletions src/style.module.css.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare const styles: {
readonly "calendar": string;
readonly "caption_label": string;
readonly "chevron": string;
readonly "chevron_disabled": string;
readonly "day": string;
readonly "disabled": string;
readonly "dropdown": string;
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {

import type { Locale } from "date-fns";

import { UI, DayModifier, CalendarFlag } from "./UI";
import { UI, DayModifier, CalendarFlag, ChevronFlag } from "./UI";
import { CalendarDay } from "./classes";
import * as components from "./components/custom-components";
import {
Expand Down Expand Up @@ -579,6 +579,8 @@ export type ClassNames = {
[state in DayModifier]: string;
} & {
[flag in CalendarFlag]: string;
} & {
[flag in ChevronFlag]: string;
};

/** The modifiers that are internally used by DayPicker. */
Expand Down

0 comments on commit b06d537

Please sign in to comment.