Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 24dc0f3

Browse files
authored
[UI]: initial variant typing and theming capabilities. (#200)
1 parent 27b5854 commit 24dc0f3

File tree

19 files changed

+178
-29
lines changed

19 files changed

+178
-29
lines changed

.changeset/nice-guests-doubt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@resembli/ui": patch
3+
"@resembli/variants-types": patch
4+
---
5+
6+
Added the concept of a variant provider and variant theming.

packages/ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"sideEffects": false,
4343
"dependencies": {
4444
"@resembli/hooks": "workspace:^0.0.1",
45+
"@resembli/variants-types": "workspace:^0.0.0",
4546
"@stitches/core": "^1.2.7"
4647
}
4748
}

packages/ui/src/components/Box.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from "react"
2+
3+
import type { ComponentVariants } from "@resembli/variants-types"
4+
5+
import { useVariantCss } from "../providers/VariantProvider"
6+
7+
export type BoxProps = ComponentVariants["Box"]
8+
9+
export function Box(props: BoxProps) {
10+
const sx = useVariantCss("Box")
11+
return <div className={sx && sx({ ...(props as Record<string, unknown>) })}>Lee</div>
12+
}

packages/ui/src/components/Collapse.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { css, keyframes } from "@stitches/core"
12
import * as React from "react"
23

3-
import { css, keyframes } from "../css/css"
4-
54
const openAnimation = keyframes({
65
"0%": { height: 0 },
76
"100%": { height: "var(--internal_height)" },

packages/ui/src/components/Tree/TreeNode.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { css } from "@stitches/core"
12
import * as React from "react"
23

34
import { useToggle } from "@resembli/hooks"
45

5-
import { css } from "../../css/css.js"
66
import { Collapse } from "../Collapse.js"
77
import { useTreeContext } from "./TreeRoot.js"
88

packages/ui/src/components/Tree/TreeRoot.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { css } from "@stitches/core"
12
import * as React from "react"
23

3-
import { css } from "../../css/css.js"
4-
54
const TreeRootCss = css({
65
margin: 0,
76
padding: 0,

packages/ui/src/css/css.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/ui/src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { CssComponent } from "@stitches/core/types/styled-component"
2+
13
export { Collapse } from "./components/Collapse.js"
24
export type { CollapseProps } from "./components/Collapse.js"
35

@@ -7,4 +9,13 @@ export type { TreeRootProps } from "./components/Tree/TreeRoot.js"
79
export { TreeNode } from "./components/Tree/TreeNode.js"
810
export type { TreeNodeProps } from "./components/Tree/TreeNode.js"
911

10-
export { css, keyframes } from "./css/css"
12+
export { Box } from "./components/Box.js"
13+
export type { BoxProps } from "./components/Box.js"
14+
15+
// Styling and theming exports
16+
export { VariantProvider } from "./providers/VariantProvider.js"
17+
export * from "@stitches/core"
18+
export type ExtractVariant<T> = T extends CssComponent<unknown, infer K> ? K : never
19+
export type MakeComponentVariants<T extends { Box: unknown }> = {
20+
[k in keyof T]: ExtractVariant<T[k]>
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { css } from "@stitches/core"
2+
import * as React from "react"
3+
4+
interface ComponentsType {
5+
Box?: unknown
6+
}
7+
8+
const VariantContext = React.createContext<ComponentsType>({})
9+
10+
export function VariantProvider({
11+
value,
12+
children,
13+
}: React.PropsWithChildren<{ value: ComponentsType }>) {
14+
return <VariantContext.Provider value={value}>{children}</VariantContext.Provider>
15+
}
16+
17+
export function useVariantCss(componentKey: keyof ComponentsType) {
18+
return React.useContext(VariantContext)[componentKey] as ReturnType<typeof css>
19+
}

packages/variants/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ComponentVariants {
2+
[k: string]: unknown
3+
}

0 commit comments

Comments
 (0)