@patternfly/documentation-framework 6.0.10 → 6.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 6.0.12 (2024-11-22)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * improve handling of dark mode preferences ([#4389](https://github.com/patternfly/patternfly-org/issues/4389)) ([1580900](https://github.com/patternfly/patternfly-org/commit/1580900ab1e27bdc3373d91e97c521da30878ac5)), closes [#4365](https://github.com/patternfly/patternfly-org/issues/4365)
12
+
13
+
14
+
15
+
16
+
17
+ ## 6.0.11 (2024-11-22)
18
+
19
+
20
+ ### Reverts
21
+
22
+ * Revert "chore: upgrade Yarn to the latest version (#4387)" (#4391) ([14a81bf](https://github.com/patternfly/patternfly-org/commit/14a81bf15e81a7faa8ebea4c44e4fca2c6fae964)), closes [#4387](https://github.com/patternfly/patternfly-org/issues/4387) [#4391](https://github.com/patternfly/patternfly-org/issues/4391)
23
+
24
+
25
+
26
+
27
+
6
28
  ## 6.0.10 (2024-11-21)
7
29
 
8
30
  **Note:** Version bump only for package @patternfly/documentation-framework
@@ -1,4 +1,4 @@
1
- import React, { useEffect, useState, createContext } from 'react';
1
+ import React, { useEffect, useState, createContext, useCallback } from 'react';
2
2
  import {
3
3
  Button,
4
4
  Page,
@@ -232,6 +232,26 @@ export function attachDocSearch(algolia, inputSelector, timeout) {
232
232
  }
233
233
  }
234
234
 
235
+ const DARK_MODE_CLASS = "pf-v6-theme-dark";
236
+ const DARK_MODE_STORAGE_KEY = "dark-mode";
237
+
238
+ /**
239
+ * Determines if dark mode is enabled based on the stored value or the system preference.
240
+ * @returns {boolean} true if dark mode is enabled, false otherwise
241
+ */
242
+ function isDarkModeEnabled() {
243
+ // When running in prerender mode we can't access the browser APIs.
244
+ if (process.env.PRERENDER) {
245
+ return false;
246
+ }
247
+
248
+ const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
249
+ const storedValue = localStorage.getItem(DARK_MODE_STORAGE_KEY);
250
+ const isEnabled = storedValue === null ? mediaQuery.matches : storedValue === "true";
251
+
252
+ return isEnabled;
253
+ }
254
+
235
255
  export const SideNavLayout = ({ children, groupedRoutes, navOpen: navOpenProp }) => {
236
256
  const algolia = process.env.algolia;
237
257
  const hasGdprBanner = process.env.hasGdprBanner;
@@ -245,7 +265,63 @@ export const SideNavLayout = ({ children, groupedRoutes, navOpen: navOpenProp })
245
265
 
246
266
  const [versions, setVersions] = useState({ ...staticVersions });
247
267
  const [isRTL, setIsRTL] = useState(false);
248
- const [isDarkTheme, setIsDarkTheme] = React.useState(false);
268
+ const [isDarkTheme, setIsDarkThemeInternal] = useState(() => isDarkModeEnabled());
269
+
270
+ /**
271
+ * Stores the dark mode preference in local storage and updates the dark mode class.
272
+ */
273
+ const setIsDarkTheme = useCallback((value) => {
274
+ localStorage.setItem(DARK_MODE_STORAGE_KEY, value.toString());
275
+ updateDarkMode();
276
+ }, []);
277
+
278
+ /**
279
+ * Updates the dark mode class to the root element depending on whether dark mode is enabled.
280
+ */
281
+ const updateDarkMode = useCallback(() => {
282
+ const isEnabled = isDarkModeEnabled();
283
+ const root = document.documentElement;
284
+
285
+ if (isEnabled) {
286
+ root.classList.add(DARK_MODE_CLASS);
287
+ } else {
288
+ root.classList.remove(DARK_MODE_CLASS);
289
+ }
290
+
291
+ setIsDarkThemeInternal(isEnabled);
292
+ }, []);
293
+
294
+ useEffect(() => {
295
+ // When running in prerender mode we can't access the browser APIs.
296
+ if (process.env.PRERENDER) {
297
+ return;
298
+ }
299
+
300
+ // Update the dark mode when the the user changes their system/browser preference.
301
+ const onQueryChange = () => {
302
+ // Remove the stored value to defer to the system preference.
303
+ localStorage.removeItem(DARK_MODE_STORAGE_KEY);
304
+ updateDarkMode();
305
+ };
306
+
307
+ // Update the dark mode when the user changes the preference in another context (e.g. tab or window).
308
+ /** @type {(event: StorageEvent) => void} */
309
+ const onStorageChange = (event) => {
310
+ if (event.key === DARK_MODE_STORAGE_KEY) {
311
+ updateDarkMode();
312
+ }
313
+ };
314
+
315
+ const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
316
+
317
+ mediaQuery.addEventListener("change", onQueryChange);
318
+ window.addEventListener("storage", onStorageChange);
319
+
320
+ return () => {
321
+ mediaQuery.removeEventListener("change", onQueryChange);
322
+ window.removeEventListener("storage", onStorageChange);
323
+ };
324
+ }, []);
249
325
 
250
326
  useEffect(() => {
251
327
  if (typeof window === 'undefined') {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@patternfly/documentation-framework",
3
3
  "description": "A framework to build documentation for PatternFly.",
4
- "version": "6.0.10",
4
+ "version": "6.0.12",
5
5
  "author": "Red Hat",
6
6
  "license": "MIT",
7
7
  "private": false,
@@ -13,7 +13,7 @@
13
13
  "@babel/preset-env": "^7.24.3",
14
14
  "@babel/preset-react": "^7.24.1",
15
15
  "@mdx-js/util": "1.6.16",
16
- "@patternfly/ast-helpers": "^1.4.0-alpha.126",
16
+ "@patternfly/ast-helpers": "^1.4.0-alpha.128",
17
17
  "@reach/router": "npm:@gatsbyjs/reach-router@1.3.9",
18
18
  "autoprefixer": "9.8.6",
19
19
  "babel-loader": "^9.1.3",
@@ -80,5 +80,5 @@
80
80
  "react": "^17.0.0 || ^18.0.0",
81
81
  "react-dom": "^17.0.0 || ^18.0.0"
82
82
  },
83
- "gitHead": "3d437f9ad6d0307fdab5e76995bd395c9aeed10e"
83
+ "gitHead": "f66211d8649d3f8e308ed1b2cb31f519206f9233"
84
84
  }
@@ -3,6 +3,7 @@
3
3
  <head>
4
4
  <meta charset="utf-8">
5
5
  <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
6
+ <meta name="color-scheme" content="light dark">
6
7
  <meta name="description" content="PatternFly is Red Hat's open source design system. It consists of components, documentation, and code for building enterprise applications at scale.">
7
8
  <title><%= title %></title>
8
9
  <link rel="shortcut icon" href="/assets/Favicon-Light.png">
@@ -13,6 +14,41 @@
13
14
  <meta name="mobile-web-app-capable" content="yes">
14
15
  <meta name="theme-color" content="#151515">
15
16
  <meta name="application-name" content="PatternFly docs">
17
+ <script>
18
+ (() => {
19
+ "use strict";
20
+
21
+ const DARK_MODE_CLASS = "pf-v6-theme-dark";
22
+ const DARK_MODE_STORAGE_KEY = "dark-mode";
23
+ const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
24
+
25
+ // Ensure that the dark mode is correctly set before the page starts rendering.
26
+ updateDarkMode();
27
+
28
+ /**
29
+ * Applies the dark mode class to the root element if dark mode is enabled.
30
+ */
31
+ function updateDarkMode() {
32
+ const isEnabled = isDarkModeEnabled();
33
+ const root = document.documentElement;
34
+
35
+ if (isEnabled) {
36
+ root.classList.add(DARK_MODE_CLASS);
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Determines if dark mode is enabled based on the stored value or the system preference.
42
+ * @returns {boolean} true if dark mode is enabled, false otherwise
43
+ */
44
+ function isDarkModeEnabled() {
45
+ const storedValue = localStorage.getItem(DARK_MODE_STORAGE_KEY);
46
+ const isEnabled = storedValue === null ? darkModeQuery.matches : storedValue === "true";
47
+
48
+ return isEnabled;
49
+ }
50
+ })();
51
+ </script>
16
52
  <%= htmlWebpackPlugin.tags.headTags %>
17
53
  </head>
18
54
  <body>