Skip to content

Commit dfae938

Browse files
committed
code review
1 parent d40c396 commit dfae938

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed

src/generators/jsx-ast/utils/buildContent.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const transformStabilityNode = (node, index, parent) => {
9595
parent.children[index] = createJSXElement(JSX_IMPORTS.AlertBox.name, {
9696
children: sliceMarkdown(node, start, getTreeLength(node), {
9797
trimWhitespace: true,
98-
}).children,
98+
}).children[0].children,
9999
level: STABILITY_LEVELS[parseInt(node.data.index)],
100100
title: node.data.index,
101101
});

src/generators/web/template.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html lang="en">
33
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
46
<title>{{title}}</title>
57
<link rel="stylesheet" href="styles.css" />
8+
9+
<!-- Apply theme before paint to avoid FOUC (Flash of Unstyled Content) -->
610
<script>
7-
// This script runs before the document renders
811
document.documentElement.setAttribute('data-theme', localStorage.getItem('theme') ||
912
(matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'));
1013
</script>

src/generators/web/ui/components/NavBar.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default () => {
2020
navItems={[]}
2121
>
2222
{/* TODO(@avivkeller): Orama doesn't support Server-Side rendering yet */}
23-
{CLIENT && <SearchBox />}
23+
{CLIENT && <SearchBox theme={theme} />}
2424
<ThemeToggle
2525
onClick={toggleTheme}
2626
aria-label={`Switch to ${theme === 'light' ? 'dark' : 'light'} theme`}

src/generators/web/ui/components/SearchBox/index.jsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { OramaSearchButton, OramaSearchBox } from '@orama/react-components';
33
import { useState, useEffect } from 'react';
44

55
import { themeConfig } from './config.mjs';
6-
import { useTheme } from '../../hooks/useTheme.mjs';
76

87
/**
9-
* Search component that provides documentation search functionality using Orama
8+
* Search component that provides documentation search functionality using Orama.
9+
*
10+
* @param {{ theme: string }} props - Component props.
1011
*/
11-
const MDXSearchBox = () => {
12-
const [colorScheme] = useTheme();
12+
const SearchBox = ({ theme }) => {
1313
const [client, setClient] = useState(null);
1414
const [isLoading, setIsLoading] = useState(true);
1515

@@ -23,10 +23,8 @@ const MDXSearchBox = () => {
2323
schema: {},
2424
});
2525

26-
// Set the client immediately so it's available
2726
setClient(db);
2827

29-
// Then fetch and load the data
3028
const response = await fetch('orama-db.json');
3129
if (response.ok) {
3230
load(db, await response.json());
@@ -44,7 +42,7 @@ const MDXSearchBox = () => {
4442
<OramaSearchButton
4543
aria-disabled={!client || isLoading}
4644
style={{ flexGrow: 1 }}
47-
colorScheme={colorScheme}
45+
colorScheme={theme}
4846
themeConfig={themeConfig}
4947
aria-label="Search documentation"
5048
>
@@ -55,11 +53,11 @@ const MDXSearchBox = () => {
5553
disableChat={true}
5654
linksTarget="_self"
5755
clientInstance={client}
58-
colorScheme={colorScheme}
56+
colorScheme={theme}
5957
themeConfig={themeConfig}
6058
/>
6159
</>
6260
);
6361
};
6462

65-
export default MDXSearchBox;
63+
export default SearchBox;
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
1-
import { useState } from 'react';
1+
import { useState, useEffect, useCallback } from 'react';
22

33
/**
4-
* This hook provides theme management functionality
4+
* Applies the given theme to the <html> element and persists it in localStorage.
5+
*
6+
* @param {string} theme - The theme to apply ('light' or 'dark').
7+
*/
8+
const applyTheme = theme => {
9+
document.documentElement.setAttribute('data-theme', theme);
10+
localStorage.setItem('theme', theme);
11+
};
12+
13+
/**
14+
* A React hook for managing light/dark theme.
515
*/
616
export const useTheme = () => {
7-
const [theme, setTheme] = useState(() =>
8-
CLIENT ? document.documentElement.getAttribute('data-theme') : 'light'
9-
);
17+
const [theme, setTheme] = useState('light');
18+
19+
useEffect(() => {
20+
const initial =
21+
localStorage.getItem('theme') ||
22+
document.documentElement.getAttribute('data-theme') ||
23+
'light';
24+
applyTheme(initial);
25+
setTheme(initial);
26+
}, []);
1027

1128
/**
12-
* Toggles the theme between 'light' and 'dark'.
29+
* Toggle between 'light' and 'dark' themes.
1330
*/
14-
const toggleTheme = () => {
15-
const newTheme = theme === 'light' ? 'dark' : 'light';
16-
setTheme(newTheme);
17-
document.documentElement.setAttribute('data-theme', newTheme);
18-
localStorage.setItem('theme', newTheme);
19-
};
31+
const toggleTheme = useCallback(() => {
32+
setTheme(prev => {
33+
const next = prev === 'light' ? 'dark' : 'light';
34+
applyTheme(next);
35+
return next;
36+
});
37+
}, []);
2038

2139
return [theme, toggleTheme];
2240
};

0 commit comments

Comments
 (0)