Skip to content

Commit

Permalink
feat: Support multiple tags for server-target
Browse files Browse the repository at this point in the history
- Use case: replace html element in case SSR is preferred over SSG.
  • Loading branch information
mayank1513 committed Jun 13, 2024
1 parent e6395c6 commit 59dbc4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-mayflies-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-darkmode": minor
---

Support passing tag to server-target. Use case: replace html element in case SSR is preferred over SSG.
22 changes: 20 additions & 2 deletions lib/src/server/server-target/server-target.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { cookies } from "next/headers";
import { COOKIE_KEY, LIGHT } from "../../constants";
import { HTMLProps } from "react";

export interface ServerTargetProps extends HTMLProps<HTMLHtmlElement | HTMLDivElement> {
tag?: "html" | "div";
}

/**
* Server Side target for avoiding flash of un-themed content.
Expand All @@ -15,9 +20,22 @@ import { COOKIE_KEY, LIGHT } from "../../constants";
* </html>
* ```
*
* If you prefer SSR over SSG, you can replace `html` element with `ServerTarget`. This way you can avoid having to write sybling selectors.
*
* @example
* ```tsx
* <ServerTarget tag="html">
* ...
* </ServerTarget>
* ```
*
* @param tag - Tag to use for the target. Defaults to `div`.
* ```
*
* @source - Source code
*/
export const ServerTarget = () => {
export const ServerTarget = ({ tag: Tag = "div", ...props }: ServerTargetProps) => {
const rm = cookies().get(COOKIE_KEY)?.value ?? LIGHT;
return <div className={rm} data-rm={rm} data-ndm="ndm" data-testid="server-target" />;
// @ts-expect-error -- too complex types
return <Tag className={rm} data-rm={rm} data-ndm="ndm" data-testid="server-target" {...props} />;
};

0 comments on commit 59dbc4f

Please sign in to comment.