-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorSettingsContext.tsx
More file actions
34 lines (28 loc) · 1.14 KB
/
AuthorSettingsContext.tsx
File metadata and controls
34 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import {merge} from '@primer/styled-react'
import type React from 'react'
import {createContext, use} from 'react'
export type AuthorSettings = {
fontWeight: 'normal' | 'semibold' | 'bold'
fontColor: 'fg.default' | 'fg.muted'
includeTooltip: boolean
avatarSize: 16 | 20 | undefined
}
const defaultSettings: AuthorSettings = {
fontWeight: 'bold' as const,
fontColor: 'fg.default' as const,
includeTooltip: false,
avatarSize: undefined, // defaults to primer component default
}
const AuthorSettingsContext = createContext<AuthorSettings>(defaultSettings)
export function AuthorSettingsProvider({
authorSettings,
children,
}: React.PropsWithChildren<{authorSettings: Partial<AuthorSettings> | undefined}>) {
const authorSettingsOrDefault = merge(defaultSettings, authorSettings ?? {})
return <AuthorSettingsContext value={authorSettingsOrDefault}>{children}</AuthorSettingsContext>
}
export function useAuthorSettings() {
return use(AuthorSettingsContext) || defaultSettings
}
try{ AuthorSettingsContext.displayName ||= 'AuthorSettingsContext' } catch {}
try{ AuthorSettingsProvider.displayName ||= 'AuthorSettingsProvider' } catch {}