mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
feat: update package.json exports to support new module formats and types
This commit is contained in:
45
vue/primitives/src/config-provider/context.ts
Normal file
45
vue/primitives/src/config-provider/context.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ref, shallowRef, toValue } from 'vue';
|
||||
import type { App, MaybeRefOrGetter, Ref, ShallowRef, UnwrapRef } from 'vue';
|
||||
import { useContextFactory } from '@robonen/vue';
|
||||
|
||||
export type Direction = 'ltr' | 'rtl';
|
||||
|
||||
export interface ConfigContext {
|
||||
dir: Ref<Direction>;
|
||||
nonce: Ref<string | undefined>;
|
||||
teleportTarget: ShallowRef<string | HTMLElement>;
|
||||
}
|
||||
|
||||
export interface ConfigOptions {
|
||||
dir?: MaybeRefOrGetter<Direction>;
|
||||
nonce?: MaybeRefOrGetter<string | undefined>;
|
||||
teleportTarget?: MaybeRefOrGetter<string | HTMLElement>;
|
||||
}
|
||||
|
||||
const DEFAULT_CONFIG: UnwrapRef<ConfigContext> = {
|
||||
dir: 'ltr',
|
||||
nonce: undefined,
|
||||
teleportTarget: 'body',
|
||||
};
|
||||
|
||||
const ConfigCtx = useContextFactory<ConfigContext>('ConfigContext');
|
||||
|
||||
function resolveContext(options?: ConfigOptions): ConfigContext {
|
||||
return {
|
||||
dir: ref(toValue(options?.dir) ?? DEFAULT_CONFIG.dir),
|
||||
nonce: ref(toValue(options?.nonce) ?? DEFAULT_CONFIG.nonce),
|
||||
teleportTarget: shallowRef(toValue(options?.teleportTarget) ?? DEFAULT_CONFIG.teleportTarget),
|
||||
};
|
||||
}
|
||||
|
||||
export function provideConfig(options?: ConfigOptions): ConfigContext {
|
||||
return ConfigCtx.provide(resolveContext(options));
|
||||
}
|
||||
|
||||
export function provideAppConfig(app: App, options?: ConfigOptions): ConfigContext {
|
||||
return ConfigCtx.appProvide(app)(resolveContext(options));
|
||||
}
|
||||
|
||||
export function useConfig(): ConfigContext {
|
||||
return ConfigCtx.inject(resolveContext());
|
||||
}
|
||||
Reference in New Issue
Block a user