Skip to content

Commit

Permalink
Better support for React 19 (#3268)
Browse files Browse the repository at this point in the history
* Date/MonthPicker: Upgrade react-day-picker to fix issue with Nextjs 15
* Slot: Use ref from props to avoid warning in React 19
  • Loading branch information
HalvorHaugan authored Oct 24, 2024
1 parent 59dc3e9 commit fdc3244
Show file tree
Hide file tree
Showing 11 changed files with 1,358 additions and 782 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-tomatoes-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Date/MonthPicker: Upgrade react-day-picker to fix issue with React 19
5 changes: 5 additions & 0 deletions .changeset/red-months-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Avoid warning about element.ref in React 19
2 changes: 1 addition & 1 deletion @navikt/core/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@
"@navikt/ds-tokens": "^7.3.1",
"clsx": "^2.1.0",
"date-fns": "^3.0.0",
"react-day-picker": "8.10.0"
"react-day-picker": "8.10.1"
},
"devDependencies": {
"@testing-library/dom": "9.3.4",
Expand Down
11 changes: 8 additions & 3 deletions @navikt/core/react/src/slot/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ const Slot = React.forwardRef<HTMLElement, SlotProps>((props, forwardedRef) => {
const { children, ...slotProps } = props;

if (React.isValidElement(children)) {
const childRef = Object.prototype.propertyIsEnumerable.call(
children.props,
"ref",
)
? children.props.ref // React 19 (children.ref still works, but gives a warning)
: (children as any).ref; // React <19

return React.cloneElement<any>(children, {
...mergeProps(slotProps, children.props),
ref: forwardedRef
? mergeRefs([forwardedRef, (children as any).ref])
: (children as any).ref,
ref: forwardedRef ? mergeRefs([forwardedRef, childRef]) : childRef,
});
}

Expand Down
7 changes: 4 additions & 3 deletions examples/next-appdir/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "rimraf .next && yarn && next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand All @@ -12,15 +12,16 @@
"@navikt/aksel-icons": "file:./../../@navikt/aksel-icons",
"@navikt/ds-css": "file:./../../@navikt/core/css",
"@navikt/ds-react": "file:./../../@navikt/core/react",
"@next/bundle-analyzer": "^14.1.0",
"next": "14.2.12",
"@next/bundle-analyzer": "^15.0.0",
"next": "15.0.0",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"rimraf": "^6.0.1",
"typescript": "5.5.4"
}
}
18 changes: 18 additions & 0 deletions examples/next-appdir/src/app/client.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use client";

import { ExpansionCard } from "@navikt/ds-react";
import { Box } from "@navikt/ds-react/Box";
import { DatePicker, useDatepicker } from "@navikt/ds-react/DatePicker";
import { omit, useClientLayoutEffect } from "@navikt/ds-react/Utils";

export const ClientComponent = () => {
Expand All @@ -21,3 +23,19 @@ export const ClientComponent = () => {
</div>
);
};

export const MyDatePicker = () => {
const { datepickerProps, inputProps, selectedDay } = useDatepicker({
fromDate: new Date("Aug 23 2019"),
onDateChange: console.info,
});

return (
<div>
<DatePicker {...datepickerProps}>
<DatePicker.Input {...inputProps} label="Velg dato" />
</DatePicker>
<Box paddingBlock="4 0">{selectedDay?.toLocaleDateString()}</Box>
</div>
);
};
24 changes: 0 additions & 24 deletions examples/next-appdir/src/app/page.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,6 @@
padding: 4rem 0;
}

.center::before {
background: var(--secondary-glow);
border-radius: 50%;
width: 480px;
height: 360px;
margin-left: -400px;
}

.center::after {
background: var(--primary-glow);
width: 240px;
height: 180px;
z-index: -1;
}

.center::before,
.center::after {
content: "";
left: 50%;
position: absolute;
filter: blur(45px);
transform: translateZ(0);
}

.logo {
position: relative;
}
Expand Down
5 changes: 4 additions & 1 deletion examples/next-appdir/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { Select } from "@navikt/ds-react/Select";
import { Tooltip } from "@navikt/ds-react/Tooltip";
import { omit } from "@navikt/ds-react/Utils";
import { ClientComponent } from "./client";
import { ClientComponent, MyDatePicker } from "./client";
import styles from "./page.module.css";

export default function Home() {
Expand Down Expand Up @@ -116,6 +116,9 @@ export default function Home() {
<Checkbox value="Midterst">Midterst</Checkbox>
<Checkbox value="Fremst">Fremst</Checkbox>
</CheckboxGroup>

<MyDatePicker />

<div className={styles.center}>
<Image
className={styles.logo}
Expand Down
3 changes: 2 additions & 1 deletion examples/next-appdir/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
],
"paths": {
"@/*": ["./src/*"]
}
},
"target": "ES2017"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
Expand Down
Loading

0 comments on commit fdc3244

Please sign in to comment.