Skip to content

Commit 223e7f9

Browse files
committed
Add force theme component to easily create page with forced theme
1 parent 633408e commit 223e7f9

File tree

7 files changed

+53
-5
lines changed

7 files changed

+53
-5
lines changed

.tkb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"J1yONFe2BVs5GbQJj1SXG": {
1515
"id": "J1yONFe2BVs5GbQJj1SXG",
1616
"description": "Add ThemeContainer server component",
17-
"columnId": "column-todo"
17+
"columnId": "column-done"
1818
},
1919
"G-1H47JpSI69kav1qZ-kp": {
2020
"id": "G-1H47JpSI69kav1qZ-kp",
@@ -27,7 +27,6 @@
2727
"id": "column-todo",
2828
"title": "To do",
2929
"tasksIds": [
30-
"J1yONFe2BVs5GbQJj1SXG",
3130
"G-1H47JpSI69kav1qZ-kp"
3231
]
3332
},
@@ -40,6 +39,7 @@
4039
"id": "column-done",
4140
"title": "Done",
4241
"tasksIds": [
42+
"J1yONFe2BVs5GbQJj1SXG",
4343
"p1R-Q4atxMo-29mSa7mb6",
4444
"-FlzW8htLo-6jz5-ZqjEx"
4545
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { cleanup, render, screen } from "@testing-library/react";
2+
import { afterEach, describe, test } from "vitest";
3+
import { ForceTheme } from "./force-theme";
4+
5+
describe("force-theme", () => {
6+
afterEach(cleanup);
7+
8+
test("check specificity class exists", ({ expect }) => {
9+
render(<ForceTheme />);
10+
expect(screen.getByTestId("force-theme").classList).toContain("nth-scoped");
11+
});
12+
13+
test("force theme", ({ expect }) => {
14+
render(<ForceTheme theme="my-theme" />);
15+
expect(screen.getByTestId("force-theme").classList).toContain("th-my-theme");
16+
});
17+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as React from "react";
2+
import type { HTMLProps } from "react";
3+
4+
interface ForceThemeProps extends HTMLProps<HTMLElement> {
5+
/** @defaultValue 'div' */
6+
tag?: keyof JSX.IntrinsicElements;
7+
children?: React.ReactNode;
8+
theme?: string;
9+
colorScheme?: "dark" | "light";
10+
/** provide styles object if you are using CSS/SCSS modules. */
11+
styles?: Record<string, string>;
12+
}
13+
14+
/**
15+
* # ForceTheme
16+
*
17+
*/
18+
export function ForceTheme({ children, tag, theme, colorScheme, styles, className, ...props }: ForceThemeProps) {
19+
let classNames = [theme ? `th-${theme}` : "", colorScheme ?? "", "nth-scoped"];
20+
if (styles) classNames = classNames.map(cls => styles[cls] ?? cls);
21+
if (className) classNames.push(className);
22+
const Tag = tag ?? "div";
23+
return (
24+
// @ts-expect-error: too complex types for props.
25+
<Tag {...props} className={classNames.join(" ")} data-testid="force-theme">
26+
{children}
27+
</Tag>
28+
);
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./force-theme";

lib/nthul/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
* */
66

77
// server component exports
8+
export * from "./force-theme";
89
export * from "./server-target";

lib/nthul/src/server/server-target/server-target.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ describe("server-target", () => {
3737
},
3838
};
3939
const styles = {
40-
[THEME]: `moduled-${THEME}`,
40+
[`th-${THEME}`]: `moduled-${THEME}`,
4141
dark: `moduled-dark`,
4242
light: `moduled-light`,
4343
};
4444
render(<ServerTarget styles={styles} />);
45-
expect(screen.getByTestId("server-target").className).toBe(`th-${styles[THEME]} ${styles[COLOR_SCHEME]} `);
45+
expect(screen.getByTestId("server-target").className).toBe(`${styles[`th-${THEME}`]} ${styles[COLOR_SCHEME]} `);
4646
});
4747
});

lib/nthul/turbo/generators/templates/component.test.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { cleanup, render, screen } from "@testing-library/react";
22
import { afterEach, describe, test } from "vitest";
33
import { {{ pascalCase name }} } from "./{{ kebabCase name }}";
44

5-
describe.concurrent("{{ kebabCase name }}", () => {
5+
describe("{{ kebabCase name }}", () => {
66
afterEach(cleanup);
77

88
test("check if h1 heading exists", ({ expect }) => {

0 commit comments

Comments
 (0)