Skip to content

Commit

Permalink
feat: implements AlphaTokenProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
sungik-choi committed Dec 16, 2024
1 parent 66c7a8f commit c870003
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use client'

import React, { useMemo } from 'react'

import { tokens } from '@channel.io/bezier-tokens/alpha'

import { type ThemeName } from '~/src/types/tokens'
import { createContext } from '~/src/utils/react'

import {
type ThemeSpecificTokens,
type TokenContextValue,
type TokenProviderProps,
} from './TokenProvider.types'

const [AlphaTokenContextProvider, useAlphaTokenContext] =
createContext<TokenContextValue | null>(null, 'AlphaTokenProvider')

Check warning on line 17 in packages/bezier-react/src/components/AlphaTokenProvider/TokenProvider.tsx

View check run for this annotation

Codecov / codecov/patch

packages/bezier-react/src/components/AlphaTokenProvider/TokenProvider.tsx#L17

Added line #L17 was not covered by tests

export { useAlphaTokenContext }

const tokenSet: Record<ThemeName, ThemeSpecificTokens> = Object.freeze({

Check warning on line 21 in packages/bezier-react/src/components/AlphaTokenProvider/TokenProvider.tsx

View check run for this annotation

Codecov / codecov/patch

packages/bezier-react/src/components/AlphaTokenProvider/TokenProvider.tsx#L21

Added line #L21 was not covered by tests
light: {
global: tokens.global,
theme: tokens.lightTheme,
},
dark: {
global: tokens.global,
theme: tokens.darkTheme,
},
})

/**
* @private
*/
export function TokenProvider({ themeName, children }: TokenProviderProps) {

Check warning on line 35 in packages/bezier-react/src/components/AlphaTokenProvider/TokenProvider.tsx

View check run for this annotation

Codecov / codecov/patch

packages/bezier-react/src/components/AlphaTokenProvider/TokenProvider.tsx#L35

Added line #L35 was not covered by tests
return (
<AlphaTokenContextProvider
value={useMemo(
() => ({

Check warning on line 39 in packages/bezier-react/src/components/AlphaTokenProvider/TokenProvider.tsx

View check run for this annotation

Codecov / codecov/patch

packages/bezier-react/src/components/AlphaTokenProvider/TokenProvider.tsx#L39

Added line #L39 was not covered by tests
themeName,
tokens: tokenSet[themeName],
}),
[themeName]
)}
>
{children}
</AlphaTokenContextProvider>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
type GlobalToken,
type ThemeName,
type ThemeToken,
} from '~/src/types/alpha-tokens'

export interface ThemeSpecificTokens {
global: GlobalToken
theme: ThemeToken
}

export interface TokenContextValue {
themeName: ThemeName
tokens: ThemeSpecificTokens
}

export interface TokenProviderProps {
themeName: ThemeName
children: React.ReactNode
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
TokenProvider as AlphaTokenProvider,
useAlphaTokenContext,
} from './TokenProvider'
export type { TokenProviderProps as AlphaTokenProviderProps } from './TokenProvider.types'
3 changes: 3 additions & 0 deletions packages/bezier-react/src/types/alpha-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
export type ThemeName = 'light' | 'dark'

export type GlobalToken = typeof tokens.global
/**
* FIXME: Separate functional and semantic tokens?
*/
export type ThemeToken = typeof tokens.lightTheme | typeof tokens.darkTheme

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

0 comments on commit c870003

Please sign in to comment.