Skip to content

Commit d557834

Browse files
committed
Make dark mode configurable
1 parent 350dbb7 commit d557834

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

src/components/FairDOElasticSearch.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ComponentType, useMemo } from "react"
1818
import "../index.css"
1919
import "../elastic-ui.css"
2020
import { TooltipProvider } from "./ui/tooltip"
21+
import { useAutoDarkMode } from "@/components/utils"
2122

2223
/**
2324
* All-in-one component for rendering an elastic search UI based on the provided configuration. Includes
@@ -28,16 +29,24 @@ export function FairDOElasticSearch({
2829
config: rawConfig,
2930
debug,
3031
resultView,
31-
facetOptionView
32+
facetOptionView,
33+
dark
3234
}: {
3335
/**
3436
* Make sure the config is either memoized or constant (defined outside any components)
3537
*/
3638
config: FairDOConfig
3739
resultView?: ComponentType<ResultViewProps>
3840
facetOptionView?: ComponentType<OptionViewProps>
41+
42+
/**
43+
* Set to true to enable dark mode. Alternatively, set class="dark" on your html or body element
44+
*/
45+
dark?: boolean
3946
debug?: boolean
4047
}) {
48+
useAutoDarkMode(dark)
49+
4150
const config = useMemo(() => {
4251
return new FairDOConfigBuilder(rawConfig)
4352
}, [rawConfig])

src/components/result/NMRResultView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export function NMRResultView({ result, debug }: { result: SearchResult; debug?:
172172
return (
173173
<div className={`rfs-m-2 rfs-rounded-lg rfs-border rfs-border-border rfs-p-4 ${exactPidMatch ? "rfs-animate-outline-ping" : ""}`}>
174174
<div className="rfs-grid rfs-grid-rows-[100px_1fr] rfs-gap-4 rfs-overflow-x-auto md:rfs-max-w-full md:rfs-grid-cols-[200px_1fr] md:rfs-grid-rows-1">
175-
<div className="rfs-flex rfs-justify-center rfs-rounded md:rfs-items-center rfs-p-2 dark:rfs-invert">
175+
<div className="rfs-flex rfs-justify-center rfs-rounded md:rfs-items-center rfs-p-2 d dark:rfs-invert">
176176
{previewImage ? (
177177
<img className="md:rfs-size-[200px]" src={previewImage} alt={`Preview for ${title}`} />
178178
) : (

src/components/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useEffect } from "react"
2+
3+
export function useAutoDarkMode(dark?: boolean) {
4+
useEffect(() => {
5+
if (dark) {
6+
document.documentElement.classList.add("dark")
7+
} else {
8+
document.documentElement.classList.remove("dark")
9+
}
10+
}, [dark])
11+
}

src/stories/NMRResultView.stories.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FairDOSearchContext } from "@/components/FairDOSearchContext"
44
import { GlobalModalProvider } from "@/components/GlobalModalProvider"
55
import { DateTime } from "luxon"
66
import { NMRResultView } from "@/components/result/NMRResultView"
7+
import { TooltipProvider } from "@/components/ui/tooltip"
78

89
const meta = {
910
component: NMRResultView,
@@ -42,9 +43,11 @@ export const Default: Story = {
4243
}
4344
}}
4445
>
45-
<GlobalModalProvider>
46-
<Story />
47-
</GlobalModalProvider>
46+
<TooltipProvider>
47+
<GlobalModalProvider>
48+
<Story />
49+
</GlobalModalProvider>
50+
</TooltipProvider>
4851
</FairDOSearchContext.Provider>
4952
)
5053
]

tailwind.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import animatePlugin from "tailwindcss-animate"
44

55
export default {
6-
darkMode: ["class"],
6+
darkMode: ["class", `[class="dark"]`],
77
content: ["./src/**/*.{ts,tsx,js,jsx,css}"],
88
prefix: "rfs-",
99
theme: {

0 commit comments

Comments
 (0)