Skip to content

Commit 96defd6

Browse files
committed
feat(components): add runtime mode
1 parent 700b0e6 commit 96defd6

File tree

8 files changed

+21
-28
lines changed

8 files changed

+21
-28
lines changed

packages/components/src/core/bootstrap.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import type { Generic, LoaderCallback, RegisterOptions } from 'adopted-style-sheets';
22
import { register as coreRegister } from 'adopted-style-sheets';
3-
import { setDevMode } from '../schema';
43
import { setCustomTagNames } from './component-names';
54
import { initializeI18n } from './i18n';
65

7-
type Environment = 'development' | 'production';
8-
96
type KoliBriOptions = RegisterOptions & {
10-
/**
11-
* The environment in which the application is running.
12-
*/
13-
environment?: Environment;
147
/**
158
* This option allows you to transform the component tag names.
169
*/
@@ -33,8 +26,6 @@ export const bootstrap = async (
3326
loaders: LoaderCallback | LoaderCallback[] | Set<LoaderCallback>,
3427
koliBriOptions?: KoliBriOptions,
3528
): Promise<void[]> => {
36-
setDevMode(koliBriOptions?.environment === 'development');
37-
3829
initializeI18n(koliBriOptions?.translation?.name ?? 'de', koliBriOptions?.translations);
3930
if (koliBriOptions?.transformTagName) {
4031
setCustomTagNames(koliBriOptions?.transformTagName);

packages/components/src/i18n.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ComponentKeys, ResourcePrefix } from './core/i18n';
22
import { getI18nInstance, initializeI18n } from './core/i18n';
3-
import { processEnv } from './schema';
3+
import { runtimeMode } from './schema';
44

55
type Options = {
66
count?: number;
@@ -14,6 +14,6 @@ export let translate = (key: TranslationKey, options?: Options) => {
1414
return i18n.translate(key, options);
1515
};
1616

17-
if (processEnv === 'test') {
17+
if (runtimeMode === 'test') {
1818
translate = (key): string => key;
1919
}

packages/components/src/schema/utils/dev.utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ export const setDocument = (value: Document): void => {
1515
DOCUMENT = value;
1616
};
1717

18-
let DEV_MODE: boolean = false;
18+
import { runtimeMode } from './reuse';
19+
20+
const DEV_MODE: boolean = runtimeMode !== 'production';
1921
let EXPERIMENTAL_MODE: boolean = false;
2022
let COLOR_CONTRAST_ANALYSIS: boolean = false;
2123

2224
export const getDevMode = (): boolean => DEV_MODE === true;
23-
export const setDevMode = (mode: boolean): void => {
24-
DEV_MODE = mode === true;
25-
};
2625

2726
export const getExperimentalMode = (): boolean => EXPERIMENTAL_MODE === true;
2827
export const setExperimentalMode = (mode: boolean): void => {

packages/components/src/schema/utils/prop.validators.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { querySelector } from 'query-selector-shadow-root';
55
import rgba from 'rgba-convert';
66
import { hex, score } from 'wcag-contrast';
77

8-
import { getDocument, getExperimentalMode, Log } from './dev.utils';
8+
import { getDevMode, getDocument, getExperimentalMode, Log } from './dev.utils';
99

1010
import type { Stringified } from '../types/common';
1111
import { devHint } from './a11y.tipps';
@@ -110,7 +110,7 @@ export const setState = <T>(component: Generic.Element.Component, propName: stri
110110
* Muss erstmal in sync bleiben, da sonst der
111111
* Tooltip nicht korrekt ausgerichtet wird.
112112
*/
113-
// if (component.hydrated === true || processEnv !== 'test') {
113+
// if (component.hydrated === true || runtimeMode !== 'test') {
114114
// clearTimeout(component.timeout as NodeJS.Timeout);
115115
// component.timeout = setTimeout(() => {
116116
// clearTimeout(component.timeout as NodeJS.Timeout);
@@ -159,6 +159,10 @@ export function watchValidator<T>(
159159
value?: T,
160160
options: WatchOptions = {},
161161
): void {
162+
if (!getDevMode()) {
163+
setState(component, propName, value ?? (options.defaultValue as T), options.hooks);
164+
return;
165+
}
162166
if (validationFunction(value)) {
163167
/**
164168
* Triff zu, wenn der Wert VALIDE ist.

packages/components/src/schema/utils/reuse.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const PROCESS_ENVS = ['development', 'production', 'test'] as const;
2-
type ProcessEnv = (typeof PROCESS_ENVS)[number];
3-
export let processEnv: ProcessEnv = 'development';
1+
const MODES = ['development', 'production', 'test'] as const;
2+
export type Mode = (typeof MODES)[number];
3+
4+
export let runtimeMode: Mode = 'development';
45
try {
5-
processEnv = process.env.NODE_ENV as ProcessEnv;
6+
runtimeMode = process.env.NODE_ENV as Mode;
67
} catch (e) {
7-
processEnv = 'production';
8+
runtimeMode = 'production';
89
}
910

1011
/**

packages/components/src/utils/align-floating-elements.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { arrow, computePosition, flip, offset, shift } from '@floating-ui/dom';
2-
import { processEnv } from '../schema';
2+
import { runtimeMode } from '../schema';
33

44
import type { AlignPropType } from '../schema';
55
type Arguments = {
@@ -9,7 +9,7 @@ type Arguments = {
99
align?: AlignPropType;
1010
};
1111
export const alignFloatingElements = async ({ floatingElement, referenceElement, arrowElement, align = 'top' }: Arguments) => {
12-
if (processEnv !== 'test') {
12+
if (runtimeMode !== 'test') {
1313
const middleware = [offset(arrowElement?.offsetHeight ?? 10), flip(), shift()];
1414
if (arrowElement) {
1515
middleware.push(arrow({ element: arrowElement }));

packages/components/src/utils/dev.utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Log, getDocument, processEnv, setColorContrastAnalysis, setDevMode, setExperimentalMode } from '../schema';
1+
import { Log, getDocument, runtimeMode, setColorContrastAnalysis, setExperimentalMode } from '../schema';
22

33
import { getWindow } from '../schema';
44
import { Env } from '@stencil/core';
@@ -8,7 +8,6 @@ const initMeta = (): void => {
88
if (meta && meta.hasAttribute('content')) {
99
const content = meta.getAttribute('content');
1010
if (typeof content === 'string') {
11-
setDevMode(content.includes('dev-mode=true'));
1211
setExperimentalMode(content.includes('experimental-mode=true'));
1312
setColorContrastAnalysis(content.includes('color-contrast-analysis=true'));
1413
}
@@ -66,7 +65,7 @@ Email: kolibri@itzbund.de
6665

6766
let nonce = (): string => Math.floor(Math.random() * 16777215).toString(16);
6867

69-
if (processEnv === 'test') {
68+
if (runtimeMode === 'test') {
7069
nonce = (): string => 'nonce';
7170
}
7271

packages/samples/react/src/react.main.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ void (async () => {
8181
])
8282
: undefined,
8383
transformTagName: ENABLE_TAG_NAME_TRANSFORMER ? tagNameTransformer : undefined,
84-
environment: process.env.NODE_ENV === 'development' ? 'development' : 'production',
8584
reflectInputValues: true,
8685
},
8786
);

0 commit comments

Comments
 (0)