@uni-design-system/uni-react 0.0.14 → 0.0.16

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.
@@ -10,3 +10,4 @@ export * from './padding';
10
10
  export * from './shadow';
11
11
  export * from './text';
12
12
  export * from './theme';
13
+ export * from './transition';
@@ -26,3 +26,4 @@ __exportStar(require("./padding"), exports);
26
26
  __exportStar(require("./shadow"), exports);
27
27
  __exportStar(require("./text"), exports);
28
28
  __exportStar(require("./theme"), exports);
29
+ __exportStar(require("./transition"), exports);
@@ -1,2 +1,3 @@
1
1
  import { Theme } from '@uni-design-system/uni-core';
2
- export declare function useTheme(): Theme;
2
+ export declare const useTheme: () => Theme;
3
+ export declare const useSwitchTheme: (defaultThemeId?: string) => [string, (themeId?: string) => void];
@@ -1,20 +1,57 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useTheme = void 0;
7
- const react_1 = __importDefault(require("react"));
29
+ exports.useSwitchTheme = exports.useTheme = void 0;
30
+ const react_1 = __importStar(require("react"));
8
31
  const theme_context_1 = __importDefault(require("./theme.context"));
9
32
  const uni_core_1 = require("@uni-design-system/uni-core");
10
- function useTheme() {
11
- const themeProps = react_1.default.useContext(theme_context_1.default);
12
- const theme = themeProps && themeProps.themes && themeProps.themeId && themeProps.themes[themeProps.themeId];
33
+ const findTheme = (props) => {
34
+ const { themeId, themes } = props;
35
+ return themes && themeId ? themes[themeId] : uni_core_1.BaseTheme;
36
+ };
37
+ const useTheme = () => {
38
+ const themeProps = (0, react_1.useContext)(theme_context_1.default);
39
+ const theme = findTheme(themeProps);
13
40
  if (process.env.NODE_ENV !== 'production') {
14
41
  // eslint-disable-next-line react-hooks/rules-of-hooks
15
42
  react_1.default.useDebugValue(theme);
16
43
  }
17
44
  // return theme || LightTheme;
18
- return theme || uni_core_1.BaseTheme;
19
- }
45
+ return theme;
46
+ };
20
47
  exports.useTheme = useTheme;
48
+ const useSwitchTheme = (defaultThemeId) => {
49
+ const [themeId, setThemeId] = (0, react_1.useState)(defaultThemeId || uni_core_1.DefaultThemeId);
50
+ const switchTheme = (themeId) => {
51
+ if (!themeId)
52
+ return;
53
+ setThemeId(themeId);
54
+ };
55
+ return [themeId, switchTheme];
56
+ };
57
+ exports.useSwitchTheme = useSwitchTheme;
@@ -1,8 +1,8 @@
1
1
  import React from 'react';
2
2
  import { Theme } from '@uni-design-system/uni-core';
3
3
  interface ThemeProviderProps {
4
- themeId: string;
5
- themes: Record<string, Theme>;
4
+ themeId?: string;
5
+ themes?: Record<string, Theme>;
6
6
  children?: React.ReactChild;
7
7
  }
8
8
  export declare const ThemeProvider: ({ children, themeId, themes }: ThemeProviderProps) => JSX.Element;
@@ -37,23 +37,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
38
  exports.ThemeProvider = void 0;
39
39
  const react_1 = __importStar(require("react"));
40
- const uni_core_1 = require("@uni-design-system/uni-core");
41
40
  const theme_context_1 = __importDefault(require("./theme.context"));
41
+ const theme_hook_1 = require("./theme.hook");
42
42
  const STORAGE_KEY = 'THEME_ID';
43
43
  const ThemeProvider = ({ children, themeId, themes }) => {
44
- const [_themeId, setThemeId] = (0, react_1.useState)(themeId);
44
+ const [_themeId, setThemeId] = (0, theme_hook_1.useSwitchTheme)(themeId);
45
45
  (0, react_1.useEffect)(() => {
46
46
  (() => __awaiter(void 0, void 0, void 0, function* () {
47
47
  const id = localStorage.getItem(STORAGE_KEY);
48
- if (id) {
49
- setThemeId(id);
50
- }
51
- else {
52
- setThemeId(uni_core_1.DefaultThemeId);
53
- }
48
+ setThemeId(id || themeId);
54
49
  }))();
55
- }, []);
56
- return (react_1.default.createElement(theme_context_1.default.Provider, { value: { themeId, themes } }, _themeId ? children : null));
50
+ }, [themeId]);
51
+ return (react_1.default.createElement(theme_context_1.default.Provider, { value: { themeId: _themeId, themes } }, _themeId ? children : null));
57
52
  };
58
53
  exports.ThemeProvider = ThemeProvider;
59
54
  /*export function withTheme(Component: ({children, theme, themes }: (any & ThemeProps)) => JSX.Element) {
@@ -10,3 +10,4 @@ export * from './padding';
10
10
  export * from './shadow';
11
11
  export * from './text';
12
12
  export * from './theme';
13
+ export * from './transition';
@@ -10,3 +10,4 @@ export * from './padding';
10
10
  export * from './shadow';
11
11
  export * from './text';
12
12
  export * from './theme';
13
+ export * from './transition';
@@ -1,2 +1,3 @@
1
1
  import { Theme } from '@uni-design-system/uni-core';
2
- export declare function useTheme(): Theme;
2
+ export declare const useTheme: () => Theme;
3
+ export declare const useSwitchTheme: (defaultThemeId?: string) => [string, (themeId?: string) => void];
@@ -1,13 +1,26 @@
1
- import React from 'react';
1
+ import React, { useContext, useState } from 'react';
2
2
  import ThemeContext from './theme.context';
3
- import { BaseTheme } from '@uni-design-system/uni-core';
4
- export function useTheme() {
5
- const themeProps = React.useContext(ThemeContext);
6
- const theme = themeProps && themeProps.themes && themeProps.themeId && themeProps.themes[themeProps.themeId];
3
+ import { BaseTheme, DefaultThemeId } from '@uni-design-system/uni-core';
4
+ const findTheme = (props) => {
5
+ const { themeId, themes } = props;
6
+ return themes && themeId ? themes[themeId] : BaseTheme;
7
+ };
8
+ export const useTheme = () => {
9
+ const themeProps = useContext(ThemeContext);
10
+ const theme = findTheme(themeProps);
7
11
  if (process.env.NODE_ENV !== 'production') {
8
12
  // eslint-disable-next-line react-hooks/rules-of-hooks
9
13
  React.useDebugValue(theme);
10
14
  }
11
15
  // return theme || LightTheme;
12
- return theme || BaseTheme;
13
- }
16
+ return theme;
17
+ };
18
+ export const useSwitchTheme = (defaultThemeId) => {
19
+ const [themeId, setThemeId] = useState(defaultThemeId || DefaultThemeId);
20
+ const switchTheme = (themeId) => {
21
+ if (!themeId)
22
+ return;
23
+ setThemeId(themeId);
24
+ };
25
+ return [themeId, switchTheme];
26
+ };
@@ -1,8 +1,8 @@
1
1
  import React from 'react';
2
2
  import { Theme } from '@uni-design-system/uni-core';
3
3
  interface ThemeProviderProps {
4
- themeId: string;
5
- themes: Record<string, Theme>;
4
+ themeId?: string;
5
+ themes?: Record<string, Theme>;
6
6
  children?: React.ReactChild;
7
7
  }
8
8
  export declare const ThemeProvider: ({ children, themeId, themes }: ThemeProviderProps) => JSX.Element;
@@ -7,24 +7,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import React, { useState, useEffect } from 'react';
11
- import { DefaultThemeId } from '@uni-design-system/uni-core';
10
+ import React, { useEffect } from 'react';
12
11
  import ThemeContext from './theme.context';
12
+ import { useSwitchTheme } from './theme.hook';
13
13
  const STORAGE_KEY = 'THEME_ID';
14
14
  export const ThemeProvider = ({ children, themeId, themes }) => {
15
- const [_themeId, setThemeId] = useState(themeId);
15
+ const [_themeId, setThemeId] = useSwitchTheme(themeId);
16
16
  useEffect(() => {
17
17
  (() => __awaiter(void 0, void 0, void 0, function* () {
18
18
  const id = localStorage.getItem(STORAGE_KEY);
19
- if (id) {
20
- setThemeId(id);
21
- }
22
- else {
23
- setThemeId(DefaultThemeId);
24
- }
19
+ setThemeId(id || themeId);
25
20
  }))();
26
- }, []);
27
- return (React.createElement(ThemeContext.Provider, { value: { themeId, themes } }, _themeId ? children : null));
21
+ }, [themeId]);
22
+ return (React.createElement(ThemeContext.Provider, { value: { themeId: _themeId, themes } }, _themeId ? children : null));
28
23
  };
29
24
  /*export function withTheme(Component: ({children, theme, themes }: (any & ThemeProps)) => JSX.Element) {
30
25
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uni-design-system/uni-react",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "description": "React component library for the UNI Design System.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",