Skip to content

Commit

Permalink
refactor(core): move ColorSchemeSetValueContext to singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
binoy14 committed Apr 29, 2024
1 parent 23e50fb commit 5db6f24
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/sanity/src/_singletons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export * from './form/studio/ValidationContext'
export * from './i18n/LocaleContext'
export * from './presence/FormFieldPresenceContext'
export * from './store/_legacy/ResourceCacheContext'
export * from './studio/ColorSchemeSetValueContext'
export * from './studio/ColorSchemeValueContext'
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'react'
import type {StudioThemeColorSchemeKey} from 'sanity'

/**
* The setter for ColorSchemeValueContext, in a separate context to avoid unnecessary re-renders
* If set to false then the UI should adjust to reflect that the Studio can't change the color scheme
* @internal
*/
export const ColorSchemeSetValueContext = createContext<
((nextScheme: StudioThemeColorSchemeKey) => void) | false | null
>(null)
3 changes: 1 addition & 2 deletions packages/sanity/src/core/studio/StudioThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import {LayerProvider, ThemeProvider} from '@sanity/ui'
import {type RootTheme} from '@sanity/ui/theme'
import {type ReactNode} from 'react'
import {ColorSchemeValueContext} from 'sanity/_singletons'
import {ColorSchemeSetValueContext, ColorSchemeValueContext} from 'sanity/_singletons'

import {defaultTheme, type StudioTheme} from '../theme'
import {useActiveWorkspace} from './activeWorkspaceMatcher'
import {ColorSchemeSetValueContext} from './colorScheme'

interface StudioThemeProviderProps {
children: ReactNode
Expand Down
12 changes: 1 addition & 11 deletions packages/sanity/src/core/studio/colorScheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@ import {DesktopIcon, MoonIcon, SunIcon} from '@sanity/icons'
import {studioTheme, type ThemeColorSchemeKey, ThemeProvider, usePrefersDark} from '@sanity/ui'
import {
type ComponentType,
createContext,
type ReactNode,
useContext,
useEffect,
useMemo,
useSyncExternalStore,
} from 'react'
import {ColorSchemeValueContext} from 'sanity/_singletons'
import {ColorSchemeSetValueContext, ColorSchemeValueContext} from 'sanity/_singletons'

import {type TFunction} from '../i18n'
import {type StudioThemeColorSchemeKey} from '../theme/types'

/**
* The setter for ColorSchemeValueContext, in a separate context to avoid unnecessary re-renders
* If set to false then the UI should adjust to reflect that the Studio can't change the color scheme
* @internal
*/
export const ColorSchemeSetValueContext = createContext<
((nextScheme: StudioThemeColorSchemeKey) => void) | false | null
>(null)

/** @internal */
function useSystemScheme(): ThemeColorSchemeKey {
const prefersDark = usePrefersDark()
Expand Down

0 comments on commit 5db6f24

Please sign in to comment.