cozy-ui 109.2.0 → 110.0.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # [110.0.0](https://github.com/cozy/cozy-ui/compare/v109.2.0...v110.0.0) (2024-06-18)
2
+
3
+
4
+ ### Features
5
+
6
+ * **CozyTheme:** Use `io.cozy.settings` to determine default type ([9c513d4](https://github.com/cozy/cozy-ui/commit/9c513d4))
7
+
8
+
9
+ ### BREAKING CHANGES
10
+
11
+ * **CozyTheme:** To use `<CozyTheme>` properly, you must have permission in your app to get `io.cozy.settings` documents. The experience would be even better with realtime on this doctypes.
12
+
1
13
  # [109.2.0](https://github.com/cozy/cozy-ui/compare/v109.1.1...v109.2.0) (2024-06-17)
2
14
 
3
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-ui",
3
- "version": "109.2.0",
3
+ "version": "110.0.0",
4
4
  "description": "Cozy apps UI SDK",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -0,0 +1,25 @@
1
+ import React from 'react'
2
+
3
+ import { useQuery, isQueryLoading, hasQueryBeenLoaded } from 'cozy-client'
4
+
5
+ import { buildSettingsInstanceQuery } from './queries'
6
+ import { DumbCozyTheme } from './index'
7
+
8
+ const CozyThemeWithQuery = props => {
9
+ const instanceQuery = buildSettingsInstanceQuery()
10
+ const { data: instance, ...instanceQueryLeft } = useQuery(
11
+ instanceQuery.definition,
12
+ instanceQuery.options
13
+ )
14
+
15
+ if (
16
+ isQueryLoading(instanceQueryLeft) &&
17
+ !hasQueryBeenLoaded(instanceQueryLeft)
18
+ ) {
19
+ return null
20
+ }
21
+
22
+ return <DumbCozyTheme {...props} settingsThemeType={instance?.colorScheme} />
23
+ }
24
+
25
+ export default CozyThemeWithQuery
@@ -24,6 +24,7 @@ const themesSupportingContext = [
24
24
  ]
25
25
 
26
26
  ;
27
+
27
28
  <DemoProvider>
28
29
  <CozyTheme variant='inverted' className='u-stack-m'>
29
30
  <Paper className='u-p-1'>
@@ -1,12 +1,14 @@
1
1
  import React, { createContext, useContext, useLayoutEffect } from 'react'
2
2
  import PropTypes from 'prop-types'
3
3
  import cx from 'classnames'
4
- import flag from 'cozy-flags'
5
4
 
5
+ import flag from 'cozy-flags'
6
6
  import log from 'cozy-logger'
7
7
 
8
+ import { isRsg } from '../../hooks/useSetFlagshipUi/helpers'
8
9
  import useMediaQuery from '../../hooks/useMediaQuery'
9
10
  import MuiCozyTheme from '../../MuiCozyTheme'
11
+ import CozyThemeWithQuery from './CozyThemeWithQuery'
10
12
 
11
13
  export const CozyThemeContext = createContext()
12
14
 
@@ -25,7 +27,13 @@ export const useCozyTheme = () => {
25
27
  return context
26
28
  }
27
29
 
28
- const CozyTheme = ({ variant, className, ignoreItself, children }) => {
30
+ const DumbCozyTheme = ({
31
+ variant,
32
+ className,
33
+ ignoreItself,
34
+ settingsThemeType,
35
+ children
36
+ }) => {
29
37
  const uiThemeType = localStorage.getItem('ui-theme-type') // use only for cozy-ui documentation and argos screenshots
30
38
  const uiThemeVariant = localStorage.getItem('ui-theme-variant') // use only for cozy-ui documentation and argos screenshots
31
39
 
@@ -36,17 +44,26 @@ const CozyTheme = ({ variant, className, ignoreItself, children }) => {
36
44
  : 'light'
37
45
  : 'light'
38
46
 
39
- const selfThemeType = uiThemeType || deviceThemeType
47
+ const selfThemeType =
48
+ uiThemeType ||
49
+ (['light', 'dark'].includes(settingsThemeType)
50
+ ? settingsThemeType
51
+ : deviceThemeType)
40
52
  const selfThemeVariant = uiThemeVariant || variant
41
53
 
42
- // add css var to body to be able to use them on it
43
- useLayoutEffect(
44
- () =>
45
- document
46
- .querySelector('body')
47
- .classList.add(`CozyTheme--${selfThemeType}-normal`), // add omits if already present
48
- [selfThemeType]
49
- )
54
+ useLayoutEffect(() => {
55
+ const negativeThemeType = selfThemeType === 'light' ? 'dark' : 'light'
56
+
57
+ // remove "negative" value because can be changed without refresh
58
+ document
59
+ .querySelector('body')
60
+ .classList.remove(`CozyTheme--${negativeThemeType}-normal`)
61
+
62
+ // add css var to body to be able to use them on it
63
+ document
64
+ .querySelector('body')
65
+ .classList.add(`CozyTheme--${selfThemeType}-normal`) // `add` omits if already present
66
+ }, [selfThemeType])
50
67
 
51
68
  return (
52
69
  <CozyThemeContext.Provider
@@ -67,18 +84,34 @@ const CozyTheme = ({ variant, className, ignoreItself, children }) => {
67
84
  </CozyThemeContext.Provider>
68
85
  )
69
86
  }
87
+ DumbCozyTheme.propTypes = CozyThemeProptypes
88
+ DumbCozyTheme.defaultProps = CozyThemeDefaultProps
89
+
90
+ const CozyTheme = props => {
91
+ const Comp =
92
+ process.env.NODE_ENV === 'test' || isRsg
93
+ ? DumbCozyTheme
94
+ : CozyThemeWithQuery
95
+
96
+ return <Comp {...props} />
97
+ }
70
98
 
71
- CozyTheme.propTypes = {
99
+ const CozyThemeProptypes = {
72
100
  variant: PropTypes.oneOf(['normal', 'inverted']),
73
101
  /** Causes this element's children to appear as if they were direct children of the element's parent, ignoring the element itself. */
74
102
  ignoreItself: PropTypes.bool,
75
103
  className: PropTypes.string,
104
+ settingsThemeType: PropTypes.string,
76
105
  children: PropTypes.node
77
106
  }
78
107
 
79
- CozyTheme.defaultProps = {
108
+ const CozyThemeDefaultProps = {
80
109
  variant: 'normal',
81
110
  ignoreItself: true
82
111
  }
112
+ CozyTheme.propTypes = CozyThemeProptypes
113
+ CozyTheme.defaultProps = CozyThemeDefaultProps
83
114
 
115
+ // export this way to help doc generates correct component and props
84
116
  export default CozyTheme
117
+ export { DumbCozyTheme }
@@ -0,0 +1,14 @@
1
+ import { Q, fetchPolicies } from 'cozy-client'
2
+
3
+ const SETTINGS_DOCTYPE = 'io.cozy.settings'
4
+
5
+ const defaultFetchPolicy = fetchPolicies.olderThan(5 * 60 * 1000)
6
+
7
+ export const buildSettingsInstanceQuery = () => ({
8
+ definition: Q(SETTINGS_DOCTYPE).getById('io.cozy.settings.instance'),
9
+ options: {
10
+ as: `${SETTINGS_DOCTYPE}/io.cozy.settings.instance`,
11
+ fetchPolicy: defaultFetchPolicy,
12
+ singleDocData: true
13
+ }
14
+ })
@@ -0,0 +1,25 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
+ var _excluded = ["data"];
4
+ import React from 'react';
5
+ import { useQuery, isQueryLoading, hasQueryBeenLoaded } from 'cozy-client';
6
+ import { buildSettingsInstanceQuery } from "cozy-ui/transpiled/react/providers/CozyTheme/queries";
7
+ import { DumbCozyTheme } from "cozy-ui/transpiled/react/providers/CozyTheme/index";
8
+
9
+ var CozyThemeWithQuery = function CozyThemeWithQuery(props) {
10
+ var instanceQuery = buildSettingsInstanceQuery();
11
+
12
+ var _useQuery = useQuery(instanceQuery.definition, instanceQuery.options),
13
+ instance = _useQuery.data,
14
+ instanceQueryLeft = _objectWithoutProperties(_useQuery, _excluded);
15
+
16
+ if (isQueryLoading(instanceQueryLeft) && !hasQueryBeenLoaded(instanceQueryLeft)) {
17
+ return null;
18
+ }
19
+
20
+ return /*#__PURE__*/React.createElement(DumbCozyTheme, _extends({}, props, {
21
+ settingsThemeType: instance === null || instance === void 0 ? void 0 : instance.colorScheme
22
+ }));
23
+ };
24
+
25
+ export default CozyThemeWithQuery;
@@ -4,8 +4,10 @@ import PropTypes from 'prop-types';
4
4
  import cx from 'classnames';
5
5
  import flag from 'cozy-flags';
6
6
  import log from 'cozy-logger';
7
+ import { isRsg } from "cozy-ui/transpiled/react/hooks/useSetFlagshipUi/helpers";
7
8
  import useMediaQuery from "cozy-ui/transpiled/react/hooks/useMediaQuery";
8
9
  import MuiCozyTheme from "cozy-ui/transpiled/react/MuiCozyTheme";
10
+ import CozyThemeWithQuery from "cozy-ui/transpiled/react/providers/CozyTheme/CozyThemeWithQuery";
9
11
  export var CozyThemeContext = /*#__PURE__*/createContext();
10
12
  export var useCozyTheme = function useCozyTheme() {
11
13
  var context = useContext(CozyThemeContext);
@@ -21,12 +23,13 @@ export var useCozyTheme = function useCozyTheme() {
21
23
  return context;
22
24
  };
23
25
 
24
- var CozyTheme = function CozyTheme(_ref) {
26
+ var DumbCozyTheme = function DumbCozyTheme(_ref) {
25
27
  var _cx;
26
28
 
27
29
  var variant = _ref.variant,
28
30
  className = _ref.className,
29
31
  ignoreItself = _ref.ignoreItself,
32
+ settingsThemeType = _ref.settingsThemeType,
30
33
  children = _ref.children;
31
34
  var uiThemeType = localStorage.getItem('ui-theme-type'); // use only for cozy-ui documentation and argos screenshots
32
35
 
@@ -35,13 +38,15 @@ var CozyTheme = function CozyTheme(_ref) {
35
38
  var isOnlyLight = !!flag('ui.darkmode.enabled'); // should be remove when dark mode is validated on all apps
36
39
 
37
40
  var deviceThemeType = useMediaQuery('(prefers-color-scheme: dark)') ? isOnlyLight ? 'dark' : 'light' : 'light';
38
- var selfThemeType = uiThemeType || deviceThemeType;
39
- var selfThemeVariant = uiThemeVariant || variant; // add css var to body to be able to use them on it
40
-
41
+ var selfThemeType = uiThemeType || (['light', 'dark'].includes(settingsThemeType) ? settingsThemeType : deviceThemeType);
42
+ var selfThemeVariant = uiThemeVariant || variant;
41
43
  useLayoutEffect(function () {
42
- return document.querySelector('body').classList.add("CozyTheme--".concat(selfThemeType, "-normal"));
43
- }, // add omits if already present
44
- [selfThemeType]);
44
+ var negativeThemeType = selfThemeType === 'light' ? 'dark' : 'light'; // remove "negative" value because can be changed without refresh
45
+
46
+ document.querySelector('body').classList.remove("CozyTheme--".concat(negativeThemeType, "-normal")); // add css var to body to be able to use them on it
47
+
48
+ document.querySelector('body').classList.add("CozyTheme--".concat(selfThemeType, "-normal")); // `add` omits if already present
49
+ }, [selfThemeType]);
45
50
  return /*#__PURE__*/React.createElement(CozyThemeContext.Provider, {
46
51
  value: {
47
52
  type: selfThemeType,
@@ -55,16 +60,29 @@ var CozyTheme = function CozyTheme(_ref) {
55
60
  }, children)));
56
61
  };
57
62
 
58
- CozyTheme.propTypes = {
63
+ DumbCozyTheme.propTypes = CozyThemeProptypes;
64
+ DumbCozyTheme.defaultProps = CozyThemeDefaultProps;
65
+
66
+ var CozyTheme = function CozyTheme(props) {
67
+ var Comp = process.env.NODE_ENV === 'test' || isRsg ? DumbCozyTheme : CozyThemeWithQuery;
68
+ return /*#__PURE__*/React.createElement(Comp, props);
69
+ };
70
+
71
+ var CozyThemeProptypes = {
59
72
  variant: PropTypes.oneOf(['normal', 'inverted']),
60
73
 
61
74
  /** Causes this element's children to appear as if they were direct children of the element's parent, ignoring the element itself. */
62
75
  ignoreItself: PropTypes.bool,
63
76
  className: PropTypes.string,
77
+ settingsThemeType: PropTypes.string,
64
78
  children: PropTypes.node
65
79
  };
66
- CozyTheme.defaultProps = {
80
+ var CozyThemeDefaultProps = {
67
81
  variant: 'normal',
68
82
  ignoreItself: true
69
83
  };
70
- export default CozyTheme;
84
+ CozyTheme.propTypes = CozyThemeProptypes;
85
+ CozyTheme.defaultProps = CozyThemeDefaultProps; // export this way to help doc generates correct component and props
86
+
87
+ export default CozyTheme;
88
+ export { DumbCozyTheme };
@@ -0,0 +1,13 @@
1
+ import { Q, fetchPolicies } from 'cozy-client';
2
+ var SETTINGS_DOCTYPE = 'io.cozy.settings';
3
+ var defaultFetchPolicy = fetchPolicies.olderThan(5 * 60 * 1000);
4
+ export var buildSettingsInstanceQuery = function buildSettingsInstanceQuery() {
5
+ return {
6
+ definition: Q(SETTINGS_DOCTYPE).getById('io.cozy.settings.instance'),
7
+ options: {
8
+ as: "".concat(SETTINGS_DOCTYPE, "/io.cozy.settings.instance"),
9
+ fetchPolicy: defaultFetchPolicy,
10
+ singleDocData: true
11
+ }
12
+ };
13
+ };