Skip to content

Commit

Permalink
🐛 Fixed dark mode for standalone html editors in Admin X (#20689)
Browse files Browse the repository at this point in the history
ref https://linear.app/tryghost/issue/DES-591
- finished wiring up the darkMode prop through the context providers 

The main impact here was that the formatting toolbar was not respecting
the dark mode settings.
  • Loading branch information
9larsons authored Jul 30, 2024
1 parent 178c98c commit f7a592e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/admin-x-design-system/src/DesignSystemApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const DesignSystemApp: React.FC<DesignSystemAppProps> = ({darkMode, fetchKoenigL

return (
<div className={appClassName} {...props}>
<DesignSystemProvider fetchKoenigLexical={fetchKoenigLexical}>
<DesignSystemProvider darkMode={darkMode} fetchKoenigLexical={fetchKoenigLexical}>
{children}
</DesignSystemProvider>
</div>
Expand Down
10 changes: 6 additions & 4 deletions apps/admin-x-design-system/src/global/form/HtmlEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface HtmlEditorProps {
placeholder?: string
nodes?: 'DEFAULT_NODES' | 'BASIC_NODES' | 'MINIMAL_NODES'
emojiPicker?: boolean;
darkMode?: boolean;
}

declare global {
Expand Down Expand Up @@ -61,7 +62,8 @@ const KoenigWrapper: React.FC<HtmlEditorProps & { editor: EditorResource }> = ({
onBlur,
placeholder,
nodes,
emojiPicker = true
emojiPicker = true,
darkMode = false
}) => {
const onError = useCallback((error: unknown) => {
try {
Expand Down Expand Up @@ -128,12 +130,12 @@ const KoenigWrapper: React.FC<HtmlEditorProps & { editor: EditorResource }> = ({

return (
<koenig.KoenigComposer
darkMode={darkMode}
nodes={koenig[nodes || 'DEFAULT_NODES']}
onError={onError}
>
<koenig.KoenigComposableEditor
className='koenig-lexical koenig-lexical-editor-input'
darkMode={false}
isSnippetsEnabled={false}
markdownTransformers={transformers[nodes || 'DEFAULT_NODES']}
placeholderClassName='koenig-lexical-editor-input-placeholder line-clamp-1'
Expand All @@ -155,14 +157,14 @@ const HtmlEditor: React.FC<HtmlEditorProps & {
className,
...props
}) => {
const {fetchKoenigLexical} = useDesignSystem();
const {fetchKoenigLexical, darkMode} = useDesignSystem();
const editorResource = useMemo(() => loadKoenig(fetchKoenigLexical), [fetchKoenigLexical]);

return <div className={className || 'w-full'}>
<div className="koenig-react-editor w-full [&_*]:!font-inherit [&_*]:!text-inherit">
<ErrorBoundary name='editor'>
<Suspense fallback={<p className="koenig-react-editor-loading">Loading editor...</p>}>
<KoenigWrapper {...props} editor={editorResource} />
<KoenigWrapper {...props} darkMode={darkMode} editor={editorResource} />
</Suspense>
</ErrorBoundary>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ interface DesignSystemContextType {
isAnyTextFieldFocused: boolean;
setFocusState: (value: boolean) => void;
fetchKoenigLexical: FetchKoenigLexical;
darkMode: boolean;
}

const DesignSystemContext = createContext<DesignSystemContextType>({
isAnyTextFieldFocused: false,
setFocusState: () => {},
fetchKoenigLexical: async () => {}
fetchKoenigLexical: async () => {},
darkMode: false
});

export const useDesignSystem = () => useContext(DesignSystemContext);
Expand All @@ -29,18 +31,19 @@ export const useFocusContext = () => {

interface DesignSystemProviderProps {
fetchKoenigLexical: FetchKoenigLexical;
darkMode: boolean;
children: React.ReactNode;
}

const DesignSystemProvider: React.FC<DesignSystemProviderProps> = ({fetchKoenigLexical, children}) => {
const DesignSystemProvider: React.FC<DesignSystemProviderProps> = ({fetchKoenigLexical, darkMode, children}) => {
const [isAnyTextFieldFocused, setIsAnyTextFieldFocused] = useState(false);

const setFocusState = (value: boolean) => {
setIsAnyTextFieldFocused(value);
};

return (
<DesignSystemContext.Provider value={{isAnyTextFieldFocused, setFocusState, fetchKoenigLexical}}>
<DesignSystemContext.Provider value={{isAnyTextFieldFocused, setFocusState, fetchKoenigLexical, darkMode}}>
<GlobalDirtyStateProvider>
<Toaster />
<NiceModal.Provider>
Expand Down

0 comments on commit f7a592e

Please sign in to comment.