@data-fair/lib-common-types 1.14.0 → 1.15.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/package.json CHANGED
@@ -1,12 +1,20 @@
1
1
  {
2
2
  "name": "@data-fair/lib-common-types",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "description": "Shared schemas and built type definitions in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "prepublishOnly": "cd .. && npm run prepublishOnly",
8
8
  "build": "cd .. && npm run build"
9
9
  },
10
+ "peerDependencies": {
11
+ "tinycolor2": "1"
12
+ },
13
+ "peerDependenciesMeta": {
14
+ "tinycolor2": {
15
+ "optional": true
16
+ }
17
+ },
10
18
  "dependencies": {
11
19
  "@data-fair/lib-validation": "^1.0.0",
12
20
  "ajv-formats": "3.0.1"
package/theme/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type Colors, type Theme } from './.type/index.js';
1
2
  export * from './.type/index.js';
2
3
  export declare const defaultTheme: {
3
4
  logo: undefined;
@@ -127,3 +128,7 @@ export declare const defaultTheme: {
127
128
  'text-admin': string;
128
129
  };
129
130
  };
131
+ export declare const getTextColorsCss: (colors: Colors, theme: string) => string;
132
+ export declare const getReadableColor: (baseColor: string, bgColors: string[], darkMode: boolean, level: "AA" | "AAA") => string;
133
+ export declare const getOnColor: (color: string) => string;
134
+ export declare const fillTheme: (theme: Theme, defaultTheme: Theme) => Required<Pick<Theme, "darkColors" | "hcColors" | "hcDarkColors">> & Theme;
package/theme/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import clone from '@data-fair/lib-utils/clone.js'
2
+ import tinycolor from 'tinycolor2'
1
3
  export * from './.type/index.js'
2
4
  export const defaultTheme = {
3
5
  logo: undefined,
@@ -129,3 +131,78 @@ export const defaultTheme = {
129
131
  'text-admin': '#FFCDD2'
130
132
  },
131
133
  }
134
+ export const getTextColorsCss = (colors, theme) => {
135
+ let css = ''
136
+ for (const color of ['primary', 'secondary', 'accent', 'error', 'info', 'success', 'warning', 'admin']) {
137
+ const key = `text-${color}`
138
+ if (colors[key]) {
139
+ css += `
140
+ .v-theme--${theme} .text-${color}:not(.v-btn--disabled) { color: ${colors[key]}!important; }`
141
+ }
142
+ }
143
+ return css
144
+ }
145
+ export const getReadableColor = (baseColor, bgColors, darkMode, level) => {
146
+ const c = tinycolor(baseColor)
147
+ while (!bgColors.every(bgColor => tinycolor.isReadable(c, bgColor, { level, size: 'small' }))) {
148
+ if (darkMode) {
149
+ if (c.getBrightness() === 255) { break }
150
+ c.brighten(1)
151
+ } else {
152
+ if (c.getBrightness() === 0) { break }
153
+ c.darken(1)
154
+ }
155
+ }
156
+ return c.toHexString()
157
+ }
158
+ export const getOnColor = (color) => {
159
+ // priority to white text if it is readable
160
+ if (tinycolor.isReadable(color, '#FFFFFF', { level: 'AA', size: 'small' })) { return '#FFFFFF' }
161
+ return tinycolor.mostReadable(color, ['#000000', '#FFFFFF']).toHexString()
162
+ }
163
+ export const fillTheme = (theme, defaultTheme) => {
164
+ const fullTheme = clone(theme)
165
+ if (fullTheme.assistedMode && fullTheme.assistedModeColors) {
166
+ if (!defaultTheme.darkColors) { throw new Error('darkColors is missing in default theme') }
167
+ if (!defaultTheme.hcColors) { throw new Error('hcColors is missing in default theme') }
168
+ if (!defaultTheme.hcDarkColors) { throw new Error('hcDarkColors is missing in default theme') }
169
+ fullTheme.assistedModeColors.primary = fullTheme.assistedModeColors.primary ?? fullTheme.colors.primary
170
+ fullTheme.assistedModeColors.secondary = fullTheme.assistedModeColors.secondary ?? fullTheme.colors.secondary
171
+ fullTheme.assistedModeColors.accent = fullTheme.assistedModeColors.accent ?? fullTheme.colors.accent
172
+ fullTheme.colors = clone(defaultTheme.colors)
173
+ fullTheme.darkColors = clone(defaultTheme.darkColors)
174
+ fullTheme.hcColors = clone(defaultTheme.hcColors)
175
+ fullTheme.hcDarkColors = clone(defaultTheme.hcDarkColors)
176
+ const customColors = {
177
+ primary: fullTheme.assistedModeColors.primary,
178
+ secondary: fullTheme.assistedModeColors.secondary,
179
+ accent: fullTheme.assistedModeColors.accent,
180
+ 'on-primary': getOnColor(fullTheme.assistedModeColors.primary),
181
+ 'on-secondary': getOnColor(fullTheme.assistedModeColors.secondary),
182
+ 'on-accent': getOnColor(fullTheme.assistedModeColors.accent)
183
+ }
184
+ Object.assign(fullTheme.colors, customColors)
185
+ Object.assign(fullTheme.darkColors, customColors)
186
+ Object.assign(fullTheme.hcColors, customColors)
187
+ Object.assign(fullTheme.hcDarkColors, customColors)
188
+ fullTheme.colors['text-primary'] = getReadableColor(fullTheme.colors.primary, [fullTheme.colors.background, fullTheme.colors.surface], false, 'AA')
189
+ fullTheme.colors['text-secondary'] = getReadableColor(fullTheme.colors.secondary, [fullTheme.colors.background, fullTheme.colors.surface], false, 'AA')
190
+ fullTheme.colors['text-accent'] = getReadableColor(fullTheme.colors.accent, [fullTheme.colors.background, fullTheme.colors.surface], false, 'AA')
191
+ fullTheme.darkColors['text-primary'] = getReadableColor(fullTheme.colors.primary, [fullTheme.darkColors.background, fullTheme.darkColors.surface], true, 'AA')
192
+ fullTheme.darkColors['text-secondary'] = getReadableColor(fullTheme.colors.secondary, [fullTheme.darkColors.background, fullTheme.darkColors.surface], true, 'AA')
193
+ fullTheme.darkColors['text-accent'] = getReadableColor(fullTheme.colors.accent, [fullTheme.darkColors.background, fullTheme.darkColors.surface], true, 'AA')
194
+ fullTheme.hcColors['text-primary'] = getReadableColor(fullTheme.colors.primary, [fullTheme.hcColors.background, fullTheme.hcColors.surface], false, 'AAA')
195
+ fullTheme.hcColors['text-secondary'] = getReadableColor(fullTheme.colors.secondary, [fullTheme.hcColors.background, fullTheme.hcColors.surface], false, 'AAA')
196
+ fullTheme.hcColors['text-accent'] = getReadableColor(fullTheme.colors.accent, [fullTheme.hcColors.background, fullTheme.hcColors.surface], false, 'AAA')
197
+ fullTheme.hcDarkColors['text-primary'] = getReadableColor(fullTheme.colors.primary, [fullTheme.hcDarkColors.background, fullTheme.hcDarkColors.surface], true, 'AAA')
198
+ fullTheme.hcDarkColors['text-secondary'] = getReadableColor(fullTheme.colors.secondary, [fullTheme.hcDarkColors.background, fullTheme.hcDarkColors.surface], true, 'AAA')
199
+ fullTheme.hcDarkColors['text-accent'] = getReadableColor(fullTheme.colors.accent, [fullTheme.hcDarkColors.background, fullTheme.hcDarkColors.surface], true, 'AAA')
200
+ } else {
201
+ fullTheme.assistedModeColors = {
202
+ primary: fullTheme.colors.primary,
203
+ secondary: fullTheme.colors.secondary,
204
+ accent: fullTheme.colors.accent,
205
+ }
206
+ }
207
+ return fullTheme
208
+ }
package/theme/schema.d.ts CHANGED
@@ -5,10 +5,16 @@ declare const _default: {
5
5
  title: string;
6
6
  required: string[];
7
7
  layout: (string | {
8
+ key: string;
9
+ if: string;
10
+ cols?: undefined;
11
+ comp?: undefined;
12
+ children?: undefined;
13
+ } | {
8
14
  key: string;
9
15
  cols: number;
16
+ if: string;
10
17
  comp?: undefined;
11
- if?: undefined;
12
18
  children?: undefined;
13
19
  } | {
14
20
  comp: string;
package/theme/schema.js CHANGED
@@ -5,9 +5,9 @@ export default {
5
5
  title: 'Thème',
6
6
  required: ['colors'],
7
7
  layout: [
8
- 'logo',
9
- { key: 'bodyFontFamilyCss', cols: 6 },
10
- { key: 'headingFontFamilyCss', cols: 6 },
8
+ { key: 'logo', if: '!context.simplifiedTheme' },
9
+ { key: 'bodyFontFamilyCss', cols: 6, if: '!context.simplifiedTheme' },
10
+ { key: 'headingFontFamilyCss', cols: 6, if: '!context.simplifiedTheme' },
11
11
  'assistedMode',
12
12
  {
13
13
  comp: 'tabs',