Skip to content

Commit

Permalink
refactor: tokens (#9203)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Sep 26, 2024
1 parent 329a782 commit 26a7a19
Show file tree
Hide file tree
Showing 29 changed files with 173 additions and 231 deletions.
14 changes: 4 additions & 10 deletions projects/addon-doc/components/navigation/navigation.providers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type {Provider} from '@angular/core';
import {InjectionToken} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import type {Event} from '@angular/router';
import {ActivatedRoute, NavigationEnd, Router, Scroll} from '@angular/router';
import {TUI_DOC_PAGES, TUI_DOC_TITLE} from '@taiga-ui/addon-doc/tokens';
import type {TuiDocRoutePages} from '@taiga-ui/addon-doc/types';
import {tuiAutoFocusOptionsProvider} from '@taiga-ui/cdk/directives/auto-focus';
import {tuiIsPresent} from '@taiga-ui/cdk/utils/miscellaneous';
import {tuiCreateToken, tuiIsPresent} from '@taiga-ui/cdk/utils/miscellaneous';
import {tuiLinkOptionsProvider} from '@taiga-ui/core/components/link';
import {tuiScrollbarOptionsProvider} from '@taiga-ui/core/components/scrollbar';
import type {Observable} from 'rxjs';
Expand All @@ -22,22 +21,17 @@ function labelsProviderFactory(pages: TuiDocRoutePages): readonly string[] {
/**
* Page title
*/
export const NAVIGATION_TITLE = new InjectionToken<Observable<string>>(
'[NAVIGATION_TITLE]',
);
export const NAVIGATION_TITLE = tuiCreateToken<Observable<string>>();

/**
* Navigation sections labels for search
*/
export const NAVIGATION_LABELS = new InjectionToken<readonly string[]>(
'[NAVIGATION_LABELS]',
);
export const NAVIGATION_LABELS = tuiCreateToken<readonly string[]>();

/**
* Navigation pages
*/
export const NAVIGATION_ITEMS: InjectionToken<readonly TuiDocRoutePages[]> =
new InjectionToken<readonly TuiDocRoutePages[]>('[NAVIGATION_ITEMS]');
export const NAVIGATION_ITEMS = tuiCreateToken<readonly TuiDocRoutePages[]>();

export const NAVIGATION_PROVIDERS: Provider[] = [
tuiAutoFocusOptionsProvider({preventScroll: true}),
Expand Down
5 changes: 3 additions & 2 deletions projects/addon-doc/components/page/page.providers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type {Provider} from '@angular/core';
import {ElementRef, InjectionToken} from '@angular/core';
import {ElementRef} from '@angular/core';
import {TUI_DOC_SEE_ALSO} from '@taiga-ui/addon-doc/tokens';
import {tuiCreateToken} from '@taiga-ui/cdk/utils/miscellaneous';

/**
* Array if related page titles
*/
export const PAGE_SEE_ALSO = new InjectionToken<readonly string[]>('[PAGE_SEE_ALSO]');
export const PAGE_SEE_ALSO = tuiCreateToken<readonly string[]>();

export const PAGE_PROVIDERS: Provider[] = [
{
Expand Down
6 changes: 2 additions & 4 deletions projects/addon-doc/tokens/code-editor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {InjectionToken} from '@angular/core';
import type {TuiCodeEditor} from '@taiga-ui/addon-doc/types';
import {tuiCreateToken} from '@taiga-ui/cdk/utils/miscellaneous';

/**
* Service for opening online IDE e.g. Stackblitz
*/
export const TUI_DOC_CODE_EDITOR = new InjectionToken<TuiCodeEditor>(
'[TUI_DOC_CODE_EDITOR]',
);
export const TUI_DOC_CODE_EDITOR = tuiCreateToken<TuiCodeEditor>();
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {VIRTUAL_SCROLL_STRATEGY} from '@angular/cdk/scrolling';
import type {Provider} from '@angular/core';
import {ChangeDetectorRef, InjectionToken, Optional} from '@angular/core';
import {ChangeDetectorRef, Optional} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import type {TuiDayRange} from '@taiga-ui/cdk/date-time';
import {tuiWatch} from '@taiga-ui/cdk/observables';
import {TuiScrollService} from '@taiga-ui/cdk/services';
import {TUI_IS_IOS} from '@taiga-ui/cdk/tokens';
import {tuiCreateToken} from '@taiga-ui/cdk/utils/miscellaneous';
import {TUI_CALENDAR_DATE_STREAM} from '@taiga-ui/kit/tokens';
import type {Observable} from 'rxjs';
import {EMPTY} from 'rxjs';
Expand All @@ -15,9 +16,7 @@ import {TuiMobileCalendarStrategy} from './mobile-calendar.strategy';
/**
* Stream for updating value
*/
export const TUI_VALUE_STREAM = new InjectionToken<Observable<TuiDayRange | null>>(
'[TUI_VALUE_STREAM]',
);
export const TUI_VALUE_STREAM = tuiCreateToken<Observable<TuiDayRange | null>>();

export const TUI_MOBILE_CALENDAR_PROVIDERS: Provider[] = [
TuiScrollService,
Expand Down
74 changes: 35 additions & 39 deletions projects/cdk/tokens/window-size.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
import {inject, InjectionToken} from '@angular/core';
import {inject} from '@angular/core';
import {WA_WINDOW} from '@ng-web-apis/common';
import {tuiTypedFromEvent} from '@taiga-ui/cdk/observables';
import {tuiCreateTokenFromFactory} from '@taiga-ui/cdk/utils';
import type {Observable} from 'rxjs';
import {map, shareReplay, startWith} from 'rxjs';

export const TUI_WINDOW_SIZE = new InjectionToken<Observable<DOMRect>>(
'[TUI_WINDOW_SIZE]',
{
factory: () => {
const w = inject(WA_WINDOW);
export const TUI_WINDOW_SIZE = tuiCreateTokenFromFactory<Observable<DOMRect>>(() => {
const w = inject(WA_WINDOW);

return tuiTypedFromEvent(w, 'resize').pipe(
startWith(null),
map(() => {
const width = Math.max(
w.document.documentElement.clientWidth || 0,
w.innerWidth || 0,
w.visualViewport?.width || 0,
);
const height = Math.max(
w.document.documentElement.clientHeight || 0,
w.innerHeight || 0,
w.visualViewport?.height || 0,
);
const rect = {
width,
height,
top: 0,
left: 0,
right: width,
bottom: height,
x: 0,
y: 0,
};

return {
...rect,
toJSON: () => JSON.stringify(rect),
};
}),
shareReplay({bufferSize: 1, refCount: true}),
return tuiTypedFromEvent(w, 'resize').pipe(
startWith(null),
map(() => {
const width = Math.max(
w.document.documentElement.clientWidth || 0,
w.innerWidth || 0,
w.visualViewport?.width || 0,
);
const height = Math.max(
w.document.documentElement.clientHeight || 0,
w.innerHeight || 0,
w.visualViewport?.height || 0,
);
},
},
);
const rect = {
width,
height,
top: 0,
left: 0,
right: width,
bottom: height,
x: 0,
y: 0,
};

return {
...rect,
toJSON: () => JSON.stringify(rect),
};
}),
shareReplay({bufferSize: 1, refCount: true}),
);
});
18 changes: 7 additions & 11 deletions projects/core/components/data-list/data-list.tokens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {Provider, TemplateRef, Type} from '@angular/core';
import {InjectionToken} from '@angular/core';
import type {TuiContext, TuiIdentityMatcher, TuiStringHandler} from '@taiga-ui/cdk/types';
import {tuiProvide} from '@taiga-ui/cdk/utils/miscellaneous';
import {tuiCreateToken, tuiProvide} from '@taiga-ui/cdk/utils/miscellaneous';
import type {TuiSizeL, TuiSizeS} from '@taiga-ui/core/types';
import type {PolymorpheusContent} from '@taiga-ui/polymorpheus';

Expand All @@ -21,9 +20,10 @@ export interface TuiDataListHost<T> {
/**
* Content for tuiOption component
*/
export const TUI_OPTION_CONTENT = new InjectionToken<
PolymorpheusContent<TuiContext<TemplateRef<Record<string, unknown>>>>
>('[TUI_OPTION_CONTENT]');
export const TUI_OPTION_CONTENT =
tuiCreateToken<
PolymorpheusContent<TuiContext<TemplateRef<Record<string, unknown>>>>
>();

export function tuiAsOptionContent(
useValue: PolymorpheusContent<TuiContext<TemplateRef<Record<string, unknown>>>>,
Expand All @@ -37,9 +37,7 @@ export function tuiAsOptionContent(
/**
* Accessor for data-list options
*/
export const TUI_DATA_LIST_ACCESSOR = new InjectionToken<TuiDataListAccessor>(
'[TUI_DATA_LIST_ACCESSOR]',
);
export const TUI_DATA_LIST_ACCESSOR = tuiCreateToken<TuiDataListAccessor>();

export function tuiAsDataListAccessor(accessor: Type<TuiDataListAccessor>): Provider {
return tuiProvide(TUI_DATA_LIST_ACCESSOR, accessor);
Expand All @@ -48,9 +46,7 @@ export function tuiAsDataListAccessor(accessor: Type<TuiDataListAccessor>): Prov
/**
* DataList controller
*/
export const TUI_DATA_LIST_HOST = new InjectionToken<TuiDataListHost<unknown>>(
'[TUI_DATA_LIST_HOST]',
);
export const TUI_DATA_LIST_HOST = tuiCreateToken<TuiDataListHost<unknown>>();

export function tuiAsDataListHost<T>(host: Type<TuiDataListHost<T>>): Provider {
return tuiProvide(TUI_DATA_LIST_HOST, host);
Expand Down
8 changes: 5 additions & 3 deletions projects/core/directives/dropdown/dropdown.providers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {Type} from '@angular/core';
import {InjectionToken} from '@angular/core';
import {tuiCreateTokenFromFactory} from '@taiga-ui/cdk/utils/miscellaneous';
import {
tuiCreateToken,
tuiCreateTokenFromFactory,
} from '@taiga-ui/cdk/utils/miscellaneous';

import {TuiDropdownComponent} from './dropdown.component';

Expand All @@ -11,4 +13,4 @@ export const TUI_DROPDOWN_COMPONENT = tuiCreateTokenFromFactory<Type<any>>(
() => TuiDropdownComponent,
);

export const TUI_DROPDOWN_CONTEXT = new InjectionToken<Record<any, any>>('');
export const TUI_DROPDOWN_CONTEXT = tuiCreateToken<Record<any, any>>();
73 changes: 37 additions & 36 deletions projects/core/tokens/dark-mode.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
import {effect, inject, InjectionToken, signal, type WritableSignal} from '@angular/core';
import {effect, inject, signal, type WritableSignal} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {WA_LOCAL_STORAGE, WA_WINDOW} from '@ng-web-apis/common';
import {tuiCreateToken} from '@taiga-ui/cdk/utils/miscellaneous';
import {
tuiCreateToken,
tuiCreateTokenFromFactory,
} from '@taiga-ui/cdk/utils/miscellaneous';
import {filter, fromEvent} from 'rxjs';

export const TUI_DARK_MODE_DEFAULT_KEY = 'tuiDark';
export const TUI_DARK_MODE_KEY = tuiCreateToken(TUI_DARK_MODE_DEFAULT_KEY);
export const TUI_DARK_MODE = new InjectionToken<
export const TUI_DARK_MODE = tuiCreateTokenFromFactory<
WritableSignal<boolean> & {reset(): void}
>('', {
factory: () => {
let automatic = true;
>(() => {
let automatic = true;

const storage = inject(WA_LOCAL_STORAGE);
const key = inject(TUI_DARK_MODE_KEY);
const saved = storage.getItem(key);
const media = inject(WA_WINDOW).matchMedia('(prefers-color-scheme: dark)');
const result = signal(Boolean((saved && JSON.parse(saved)) ?? media.matches));
const storage = inject(WA_LOCAL_STORAGE);
const key = inject(TUI_DARK_MODE_KEY);
const saved = storage.getItem(key);
const media = inject(WA_WINDOW).matchMedia('(prefers-color-scheme: dark)');
const result = signal(Boolean((saved && JSON.parse(saved)) ?? media.matches));

fromEvent(media, 'change')
.pipe(
filter(() => !storage.getItem(key)),
takeUntilDestroyed(),
)
.subscribe(() => {
automatic = true;
result.set(media.matches);
});
fromEvent(media, 'change')
.pipe(
filter(() => !storage.getItem(key)),
takeUntilDestroyed(),
)
.subscribe(() => {
automatic = true;
result.set(media.matches);
});

effect(() => {
const value = String(result());
effect(() => {
const value = String(result());

if (automatic) {
automatic = false;
} else {
storage.setItem(key, value);
}
});
if (automatic) {
automatic = false;
} else {
storage.setItem(key, value);
}
});

return Object.assign(result, {
reset: () => {
storage.removeItem(key);
automatic = true;
result.set(media.matches);
},
});
},
return Object.assign(result, {
reset: () => {
storage.removeItem(key);
automatic = true;
result.set(media.matches);
},
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {DOCUMENT} from '@angular/common';
import type {Provider} from '@angular/core';
import {InjectionToken} from '@angular/core';
import {TUI_BASE_HREF} from '@taiga-ui/cdk';
import {TUI_BASE_HREF, tuiCreateToken} from '@taiga-ui/cdk';

import type {TuiVersionMeta} from './versions.constants';
import {TUI_VERSIONS_META_MAP} from './versions.constants';

export const TUI_SELECTED_VERSION_META = new InjectionToken<TuiVersionMeta | null>(
'[TUI_SELECTED_VERSION_META]: Meta information about selected version of Taiga docs',
);
export const TUI_SELECTED_VERSION_META = tuiCreateToken<TuiVersionMeta | null>();

export const TUI_VERSION_MANAGER_PROVIDERS: Provider[] = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {InjectionToken} from '@angular/core';
import {tuiCreateToken} from '@taiga-ui/cdk';

import type {TuiSupportingDocumentationComponent} from './supporting-documentation-component';

export const ABSTRACT_PROPS_ACCESSOR =
new InjectionToken<TuiSupportingDocumentationComponent>(
'[ABSTRACT_PROPS_ACCESSOR]: Component extends AbstractExample class',
);
tuiCreateToken<TuiSupportingDocumentationComponent>();
6 changes: 2 additions & 4 deletions projects/i18n/tokens/language-loader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {InjectionToken} from '@angular/core';
import {tuiCreateToken} from '@taiga-ui/cdk/utils/miscellaneous';
import type {TuiLanguageLoader} from '@taiga-ui/i18n/types';

/**
* Webpack chunk loader for Taiga UI libraries i18n
* @note: cannot be transferred to a shared file
* ReferenceError: Cannot access 'TUI_LANGUAGE_LOADER' before initialization
*/
export const TUI_LANGUAGE_LOADER = new InjectionToken<TuiLanguageLoader>(
'[TUI_LANGUAGE_LOADER]',
);
export const TUI_LANGUAGE_LOADER = tuiCreateToken<TuiLanguageLoader>();
9 changes: 2 additions & 7 deletions projects/i18n/tokens/language-storage-key.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {InjectionToken} from '@angular/core';
import {tuiCreateToken} from '@taiga-ui/cdk/utils/miscellaneous';

/**
* Default key for search value in storage
*/
export const TUI_LANGUAGE_STORAGE_KEY = new InjectionToken<string>(
'[TUI_LANGUAGE_STORAGE_KEY]',
{
factory: () => 'tuiLanguage',
},
);
export const TUI_LANGUAGE_STORAGE_KEY = tuiCreateToken<string>('tuiLanguage');
Loading

0 comments on commit 26a7a19

Please sign in to comment.