Skip to content

Commit c870003

Browse files
committed
feat: implements AlphaTokenProvider
1 parent 66c7a8f commit c870003

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use client'
2+
3+
import React, { useMemo } from 'react'
4+
5+
import { tokens } from '@channel.io/bezier-tokens/alpha'
6+
7+
import { type ThemeName } from '~/src/types/tokens'
8+
import { createContext } from '~/src/utils/react'
9+
10+
import {
11+
type ThemeSpecificTokens,
12+
type TokenContextValue,
13+
type TokenProviderProps,
14+
} from './TokenProvider.types'
15+
16+
const [AlphaTokenContextProvider, useAlphaTokenContext] =
17+
createContext<TokenContextValue | null>(null, 'AlphaTokenProvider')
18+
19+
export { useAlphaTokenContext }
20+
21+
const tokenSet: Record<ThemeName, ThemeSpecificTokens> = Object.freeze({
22+
light: {
23+
global: tokens.global,
24+
theme: tokens.lightTheme,
25+
},
26+
dark: {
27+
global: tokens.global,
28+
theme: tokens.darkTheme,
29+
},
30+
})
31+
32+
/**
33+
* @private
34+
*/
35+
export function TokenProvider({ themeName, children }: TokenProviderProps) {
36+
return (
37+
<AlphaTokenContextProvider
38+
value={useMemo(
39+
() => ({
40+
themeName,
41+
tokens: tokenSet[themeName],
42+
}),
43+
[themeName]
44+
)}
45+
>
46+
{children}
47+
</AlphaTokenContextProvider>
48+
)
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
type GlobalToken,
3+
type ThemeName,
4+
type ThemeToken,
5+
} from '~/src/types/alpha-tokens'
6+
7+
export interface ThemeSpecificTokens {
8+
global: GlobalToken
9+
theme: ThemeToken
10+
}
11+
12+
export interface TokenContextValue {
13+
themeName: ThemeName
14+
tokens: ThemeSpecificTokens
15+
}
16+
17+
export interface TokenProviderProps {
18+
themeName: ThemeName
19+
children: React.ReactNode
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
TokenProvider as AlphaTokenProvider,
3+
useAlphaTokenContext,
4+
} from './TokenProvider'
5+
export type { TokenProviderProps as AlphaTokenProviderProps } from './TokenProvider.types'

packages/bezier-react/src/types/alpha-tokens.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
export type ThemeName = 'light' | 'dark'
1111

1212
export type GlobalToken = typeof tokens.global
13+
/**
14+
* FIXME: Separate functional and semantic tokens?
15+
*/
1316
export type ThemeToken = typeof tokens.lightTheme | typeof tokens.darkTheme
1417

1518
// NOTE: (@ed) Do not remove alpha- prefix to match CSS variable names

0 commit comments

Comments
 (0)