@canonical/react-components 2.8.1 → 2.9.0

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.
@@ -0,0 +1,19 @@
1
+ import type { FC } from "react";
2
+ export declare const loadTheme: () => string;
3
+ export declare const isDarkTheme: (theme: string) => boolean;
4
+ export declare const applyTheme: (theme: string) => void;
5
+ /**
6
+ * This is a [React](https://reactjs.org/) component for the [Vanilla framework](https://docs.vanillaframework.io).
7
+ *
8
+ * The ThemeSwitcher component allows users to switch between different themes: dark, light, and system. It saves the selected theme in local storage and applies it to the document body. You can use it in user settings.
9
+ *
10
+ * In your root component, call the exported functions `loadTheme` and `applyTheme`, such as in the example below:
11
+ * ```
12
+ * useEffect(() => {
13
+ * const theme = loadTheme();
14
+ * applyTheme(theme);
15
+ * }, []);
16
+ * ```
17
+ */
18
+ declare const ThemeSwitcher: FC;
19
+ export default ThemeSwitcher;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.loadTheme = exports.isDarkTheme = exports.default = exports.applyTheme = void 0;
7
+ var _react = _interopRequireWildcard(require("react"));
8
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
9
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
10
+ const LOCAL_STORAGE_KEY = "theme";
11
+ const THEME_SYSTEM = "system";
12
+ const THEME_DARK = "dark";
13
+ const THEME_LIGHT = "light";
14
+ const loadTheme = () => {
15
+ const saved = localStorage.getItem(LOCAL_STORAGE_KEY);
16
+ return saved || THEME_SYSTEM;
17
+ };
18
+ exports.loadTheme = loadTheme;
19
+ const saveTheme = theme => {
20
+ localStorage.setItem(LOCAL_STORAGE_KEY, theme);
21
+ };
22
+ const isDarkTheme = theme => {
23
+ if (theme === THEME_SYSTEM) {
24
+ return window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
25
+ }
26
+ return theme === THEME_DARK;
27
+ };
28
+ exports.isDarkTheme = isDarkTheme;
29
+ const applyTheme = theme => {
30
+ if (isDarkTheme(theme)) {
31
+ document.body.classList.add("is-dark");
32
+ } else {
33
+ document.body.classList.remove("is-dark");
34
+ }
35
+ };
36
+
37
+ /**
38
+ * This is a [React](https://reactjs.org/) component for the [Vanilla framework](https://docs.vanillaframework.io).
39
+ *
40
+ * The ThemeSwitcher component allows users to switch between different themes: dark, light, and system. It saves the selected theme in local storage and applies it to the document body. You can use it in user settings.
41
+ *
42
+ * In your root component, call the exported functions `loadTheme` and `applyTheme`, such as in the example below:
43
+ * ```
44
+ * useEffect(() => {
45
+ * const theme = loadTheme();
46
+ * applyTheme(theme);
47
+ * }, []);
48
+ * ```
49
+ */
50
+ exports.applyTheme = applyTheme;
51
+ const ThemeSwitcher = () => {
52
+ const [activeTheme, setActiveTheme] = (0, _react.useState)(loadTheme());
53
+ const themeButton = theme => {
54
+ return /*#__PURE__*/_react.default.createElement("button", {
55
+ className: "p-segmented-control__button",
56
+ type: "button",
57
+ "aria-selected": activeTheme === theme ? "true" : "false",
58
+ onClick: () => {
59
+ saveTheme(theme);
60
+ setActiveTheme(theme);
61
+ applyTheme(theme);
62
+ }
63
+ }, theme);
64
+ };
65
+ return /*#__PURE__*/_react.default.createElement("div", {
66
+ className: "p-segmented-control"
67
+ }, /*#__PURE__*/_react.default.createElement("div", {
68
+ className: "p-segmented-control__list",
69
+ "aria-label": "Theme switcher"
70
+ }, themeButton(THEME_DARK), themeButton(THEME_LIGHT), themeButton(THEME_SYSTEM)));
71
+ };
72
+ var _default = exports.default = ThemeSwitcher;
@@ -0,0 +1,6 @@
1
+ import { Meta, StoryObj } from "@storybook/react";
2
+ import ThemeSwitcher from "./ThemeSwitcher";
3
+ declare const meta: Meta<typeof ThemeSwitcher>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof ThemeSwitcher>;
6
+ export declare const Default: Story;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.Default = void 0;
7
+ var _ThemeSwitcher = _interopRequireDefault(require("./ThemeSwitcher"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ const meta = {
10
+ component: _ThemeSwitcher.default,
11
+ tags: ["autodocs"]
12
+ };
13
+ var _default = exports.default = meta;
14
+ const Default = exports.Default = {
15
+ name: "Default"
16
+ };
@@ -0,0 +1 @@
1
+ export { default, loadTheme, isDarkTheme, applyTheme } from "./ThemeSwitcher";
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "applyTheme", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _ThemeSwitcher.applyTheme;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "default", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _ThemeSwitcher.default;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "isDarkTheme", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _ThemeSwitcher.isDarkTheme;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "loadTheme", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _ThemeSwitcher.loadTheme;
28
+ }
29
+ });
30
+ var _ThemeSwitcher = _interopRequireWildcard(require("./ThemeSwitcher"));
31
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
32
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
@@ -0,0 +1,19 @@
1
+ import type { FC } from "react";
2
+ export declare const loadTheme: () => string;
3
+ export declare const isDarkTheme: (theme: string) => boolean;
4
+ export declare const applyTheme: (theme: string) => void;
5
+ /**
6
+ * This is a [React](https://reactjs.org/) component for the [Vanilla framework](https://docs.vanillaframework.io).
7
+ *
8
+ * The ThemeSwitcher component allows users to switch between different themes: dark, light, and system. It saves the selected theme in local storage and applies it to the document body. You can use it in user settings.
9
+ *
10
+ * In your root component, call the exported functions `loadTheme` and `applyTheme`, such as in the example below:
11
+ * ```
12
+ * useEffect(() => {
13
+ * const theme = loadTheme();
14
+ * applyTheme(theme);
15
+ * }, []);
16
+ * ```
17
+ */
18
+ declare const ThemeSwitcher: FC;
19
+ export default ThemeSwitcher;
@@ -0,0 +1,62 @@
1
+ import React from "react";
2
+ import { useState } from "react";
3
+ var LOCAL_STORAGE_KEY = "theme";
4
+ var THEME_SYSTEM = "system";
5
+ var THEME_DARK = "dark";
6
+ var THEME_LIGHT = "light";
7
+ export var loadTheme = () => {
8
+ var saved = localStorage.getItem(LOCAL_STORAGE_KEY);
9
+ return saved || THEME_SYSTEM;
10
+ };
11
+ var saveTheme = theme => {
12
+ localStorage.setItem(LOCAL_STORAGE_KEY, theme);
13
+ };
14
+ export var isDarkTheme = theme => {
15
+ if (theme === THEME_SYSTEM) {
16
+ return window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
17
+ }
18
+ return theme === THEME_DARK;
19
+ };
20
+ export var applyTheme = theme => {
21
+ if (isDarkTheme(theme)) {
22
+ document.body.classList.add("is-dark");
23
+ } else {
24
+ document.body.classList.remove("is-dark");
25
+ }
26
+ };
27
+
28
+ /**
29
+ * This is a [React](https://reactjs.org/) component for the [Vanilla framework](https://docs.vanillaframework.io).
30
+ *
31
+ * The ThemeSwitcher component allows users to switch between different themes: dark, light, and system. It saves the selected theme in local storage and applies it to the document body. You can use it in user settings.
32
+ *
33
+ * In your root component, call the exported functions `loadTheme` and `applyTheme`, such as in the example below:
34
+ * ```
35
+ * useEffect(() => {
36
+ * const theme = loadTheme();
37
+ * applyTheme(theme);
38
+ * }, []);
39
+ * ```
40
+ */
41
+ var ThemeSwitcher = () => {
42
+ var [activeTheme, setActiveTheme] = useState(loadTheme());
43
+ var themeButton = theme => {
44
+ return /*#__PURE__*/React.createElement("button", {
45
+ className: "p-segmented-control__button",
46
+ type: "button",
47
+ "aria-selected": activeTheme === theme ? "true" : "false",
48
+ onClick: () => {
49
+ saveTheme(theme);
50
+ setActiveTheme(theme);
51
+ applyTheme(theme);
52
+ }
53
+ }, theme);
54
+ };
55
+ return /*#__PURE__*/React.createElement("div", {
56
+ className: "p-segmented-control"
57
+ }, /*#__PURE__*/React.createElement("div", {
58
+ className: "p-segmented-control__list",
59
+ "aria-label": "Theme switcher"
60
+ }, themeButton(THEME_DARK), themeButton(THEME_LIGHT), themeButton(THEME_SYSTEM)));
61
+ };
62
+ export default ThemeSwitcher;
@@ -0,0 +1,6 @@
1
+ import { Meta, StoryObj } from "@storybook/react";
2
+ import ThemeSwitcher from "./ThemeSwitcher";
3
+ declare const meta: Meta<typeof ThemeSwitcher>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof ThemeSwitcher>;
6
+ export declare const Default: Story;
@@ -0,0 +1,9 @@
1
+ import ThemeSwitcher from "./ThemeSwitcher";
2
+ var meta = {
3
+ component: ThemeSwitcher,
4
+ tags: ["autodocs"]
5
+ };
6
+ export default meta;
7
+ export var Default = {
8
+ name: "Default"
9
+ };
@@ -0,0 +1 @@
1
+ export { default, loadTheme, isDarkTheme, applyTheme } from "./ThemeSwitcher";
@@ -0,0 +1 @@
1
+ export { default, loadTheme, isDarkTheme, applyTheme } from "./ThemeSwitcher";
@@ -66,6 +66,7 @@ export { default as TableHeader } from "./components/TableHeader";
66
66
  export { default as TableRow } from "./components/TableRow";
67
67
  export { default as Tabs } from "./components/Tabs";
68
68
  export { default as Textarea } from "./components/Textarea";
69
+ export { default as ThemeSwitcher, loadTheme, isDarkTheme, applyTheme, } from "./components/ThemeSwitcher";
69
70
  export { ToastNotification, ToastNotificationList, ToastNotificationProvider, useToastNotification, } from "./components/Notifications";
70
71
  export { default as Tooltip } from "./components/Tooltip";
71
72
  export { default as TablePagination } from "./components/TablePagination";
package/dist/esm/index.js CHANGED
@@ -66,6 +66,7 @@ export { default as TableHeader } from "./components/TableHeader";
66
66
  export { default as TableRow } from "./components/TableRow";
67
67
  export { default as Tabs } from "./components/Tabs";
68
68
  export { default as Textarea } from "./components/Textarea";
69
+ export { default as ThemeSwitcher, loadTheme, isDarkTheme, applyTheme } from "./components/ThemeSwitcher";
69
70
  export { ToastNotification, ToastNotificationList, ToastNotificationProvider, useToastNotification } from "./components/Notifications";
70
71
  export { default as Tooltip } from "./components/Tooltip";
71
72
  export { default as TablePagination } from "./components/TablePagination";
package/dist/index.d.ts CHANGED
@@ -66,6 +66,7 @@ export { default as TableHeader } from "./components/TableHeader";
66
66
  export { default as TableRow } from "./components/TableRow";
67
67
  export { default as Tabs } from "./components/Tabs";
68
68
  export { default as Textarea } from "./components/Textarea";
69
+ export { default as ThemeSwitcher, loadTheme, isDarkTheme, applyTheme, } from "./components/ThemeSwitcher";
69
70
  export { ToastNotification, ToastNotificationList, ToastNotificationProvider, useToastNotification, } from "./components/Notifications";
70
71
  export { default as Tooltip } from "./components/Tooltip";
71
72
  export { default as TablePagination } from "./components/TablePagination";
package/dist/index.js CHANGED
@@ -87,6 +87,10 @@ var _exportNames = {
87
87
  TableRow: true,
88
88
  Tabs: true,
89
89
  Textarea: true,
90
+ ThemeSwitcher: true,
91
+ loadTheme: true,
92
+ isDarkTheme: true,
93
+ applyTheme: true,
90
94
  Tooltip: true,
91
95
  TablePagination: true,
92
96
  TablePaginationControls: true,
@@ -568,6 +572,12 @@ Object.defineProperty(exports, "Theme", {
568
572
  return _enums.Theme;
569
573
  }
570
574
  });
575
+ Object.defineProperty(exports, "ThemeSwitcher", {
576
+ enumerable: true,
577
+ get: function () {
578
+ return _ThemeSwitcher.default;
579
+ }
580
+ });
571
581
  Object.defineProperty(exports, "ToastNotification", {
572
582
  enumerable: true,
573
583
  get: function () {
@@ -592,6 +602,12 @@ Object.defineProperty(exports, "Tooltip", {
592
602
  return _Tooltip.default;
593
603
  }
594
604
  });
605
+ Object.defineProperty(exports, "applyTheme", {
606
+ enumerable: true,
607
+ get: function () {
608
+ return _ThemeSwitcher.applyTheme;
609
+ }
610
+ });
595
611
  Object.defineProperty(exports, "createEventQueue", {
596
612
  enumerable: true,
597
613
  get: function () {
@@ -610,6 +626,12 @@ Object.defineProperty(exports, "info", {
610
626
  return _NotificationProvider.info;
611
627
  }
612
628
  });
629
+ Object.defineProperty(exports, "isDarkTheme", {
630
+ enumerable: true,
631
+ get: function () {
632
+ return _ThemeSwitcher.isDarkTheme;
633
+ }
634
+ });
613
635
  Object.defineProperty(exports, "isNavigationAnchor", {
614
636
  enumerable: true,
615
637
  get: function () {
@@ -622,6 +644,12 @@ Object.defineProperty(exports, "isNavigationButton", {
622
644
  return _utils.isNavigationButton;
623
645
  }
624
646
  });
647
+ Object.defineProperty(exports, "loadTheme", {
648
+ enumerable: true,
649
+ get: function () {
650
+ return _ThemeSwitcher.loadTheme;
651
+ }
652
+ });
625
653
  Object.defineProperty(exports, "queue", {
626
654
  enumerable: true,
627
655
  get: function () {
@@ -791,6 +819,7 @@ var _TableHeader = _interopRequireDefault(require("./components/TableHeader"));
791
819
  var _TableRow = _interopRequireDefault(require("./components/TableRow"));
792
820
  var _Tabs = _interopRequireDefault(require("./components/Tabs"));
793
821
  var _Textarea = _interopRequireDefault(require("./components/Textarea"));
822
+ var _ThemeSwitcher = _interopRequireWildcard(require("./components/ThemeSwitcher"));
794
823
  var _Tooltip = _interopRequireDefault(require("./components/Tooltip"));
795
824
  var _TablePagination = _interopRequireDefault(require("./components/TablePagination"));
796
825
  var _TablePaginationControls = _interopRequireDefault(require("./components/TablePagination/TablePaginationControls"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonical/react-components",
3
- "version": "2.8.1",
3
+ "version": "2.9.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "author": {
@@ -93,7 +93,7 @@
93
93
  "tsc-alias": "1.8.10",
94
94
  "typescript": "5.7.3",
95
95
  "typescript-eslint": "8.24.1",
96
- "vanilla-framework": "4.25.0",
96
+ "vanilla-framework": "4.26.0",
97
97
  "wait-on": "8.0.2",
98
98
  "webpack": "5.98.0"
99
99
  },