Skip to content

Commit 758f604

Browse files
authored
feat: add lang attribute to DayPicker root (#2907)
1 parent 0893c96 commit 758f604

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

examples/__snapshots__/Range.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports[`should match the snapshot 1`] = `
77
class="rdp-root"
88
data-mode="range"
99
id="test"
10+
lang="en-US"
1011
>
1112
<div
1213
class="rdp-months"
@@ -646,6 +647,7 @@ exports[`when a day in the range is clicked when the day is clicked again when a
646647
class="rdp-root"
647648
data-mode="range"
648649
id="test"
650+
lang="en-US"
649651
>
650652
<div
651653
class="rdp-months"

examples/__snapshots__/StylingCssModules.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports[`should match the snapshot 1`] = `
77
<div
88
class="rdp-root"
99
data-mode="single"
10+
lang="en-US"
1011
>
1112
<div
1213
class="rdp-months"

src/DayPicker.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ test("forward aria attributes to the root element", () => {
6161
expect(dayPicker()).toHaveAttribute("aria-labelledby", "calendar-heading");
6262
});
6363

64+
describe("when rendering the root language tag", () => {
65+
test("sets the default locale code when lang is not provided", () => {
66+
render(<DayPicker data-testid={testId} />);
67+
expect(dayPicker()).toHaveAttribute("lang", defaultLocale.code);
68+
});
69+
70+
test("uses the locale code when locale is provided", () => {
71+
render(<DayPicker data-testid={testId} locale={ja} />);
72+
expect(dayPicker()).toHaveAttribute("lang", ja.code);
73+
});
74+
75+
test("prefers lang over locale code when both are provided", () => {
76+
render(<DayPicker data-testid={testId} locale={ja} lang="ar" />);
77+
expect(dayPicker()).toHaveAttribute("lang", "ar");
78+
});
79+
});
80+
6481
test("use custom components", () => {
6582
render(
6683
<DayPicker

src/DayPicker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export function DayPicker(initialProps: DayPickerProps) {
388388
style={style}
389389
dir={props.dir}
390390
id={props.id}
391-
lang={props.lang}
391+
lang={props.lang ?? locale.code}
392392
nonce={props.nonce}
393393
title={props.title}
394394
role={props.role}

src/types/props.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,12 @@ export interface PropsBase {
428428
nonce?: HTMLDivElement["nonce"];
429429
/** Add a `title` attribute to the container element. */
430430
title?: HTMLDivElement["title"];
431-
/** Add the language tag to the container element. */
431+
/**
432+
* Add the language tag to the container element.
433+
*
434+
* When omitted, DayPicker uses the active locale code (`locale.code`).
435+
* Set this prop to override the language tag.
436+
*/
432437
lang?: HTMLDivElement["lang"];
433438
/**
434439
* The locale object used to localize dates. Pass a locale from

website/docs/localization/changing-locale.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ export function Spanish() {
2626
<Examples.Spanish />
2727
</BrowserWindow>
2828

29+
## Language tag on the root element
30+
31+
DayPicker sets the root element `lang` attribute from the active locale code (`locale.code`).
32+
33+
If you need a different language tag, pass the `lang` prop explicitly.
34+
35+
```tsx
36+
import { arSA } from "react-day-picker/locale";
37+
38+
<DayPicker locale={arSA} />;
39+
```
40+
41+
You can target locale-specific styles with the CSS `:lang(...)` selector:
42+
43+
```css
44+
.rdp-root:lang(ar) {
45+
font-family: "Noto Naskh Arabic", serif;
46+
}
47+
```
48+
2949
## Numerals
3050

3151
Use the `numerals` prop to select the numbering system used by labels and formatters (for example, `latn`, `arab`, `thai`). Locale label translations also respect the numerals you choose.

0 commit comments

Comments
 (0)