Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/syncRef.md
1---2category: Reactivity3related: syncRefs4---56# syncRef78Two-way refs synchronization.910## Usage1112```ts13import { syncRef } from '@vueuse/core'1415const a = ref('a')16const b = ref('b')1718const stop = syncRef(a, b)1920console.log(a.value) // a2122b.value = 'foo'2324console.log(a.value) // foo2526a.value = 'bar'2728console.log(b.value) // bar29```3031### One directional3233```ts34import { syncRef } from '@vueuse/core'3536const a = ref('a')37const b = ref('b')3839const stop = syncRef(a, b, { direction: 'rtl' })40```4142### Custom Transform4344```ts45import { syncRef } from '@vueuse/core'4647const a = ref(10)48const b = ref(2)4950const stop = syncRef(a, b, {51transform: {52ltr: left => left * 2,53rtl: right => right / 254}55})5657console.log(b.value) // 205859b.value = 306061console.log(a.value) // 1562```6364## Type Declarations6566```ts67type Direction = "ltr" | "rtl" | "both"68type SpecificFieldPartial<T, K extends keyof T> = Partial<Pick<T, K>> &69Omit<T, K>70/**71* A = B72*/73type Equal<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false74/**75* A ∩ B ≠∅76*/77type IntersectButNotEqual<A, B> =78Equal<A, B> extends true ? false : A & B extends never ? false : true79/**80* A ⊆ B81*/82type IncludeButNotEqual<A, B> =83Equal<A, B> extends true ? false : A extends B ? true : false84/**85* A ∩ B = ∅86*/87type NotIntersect<A, B> =88Equal<A, B> extends true ? false : A & B extends never ? true : false89interface EqualType<90D extends Direction,91L,92R,93O extends keyof Transform<L, R> = D extends "both" ? "ltr" | "rtl" : D,94> {95transform?: SpecificFieldPartial<Pick<Transform<L, R>, O>, O>96}97type StrictIncludeMap<98IncludeType extends "LR" | "RL",99D extends Exclude<Direction, "both">,100L,101R,102> = Equal<[IncludeType, D], ["LR", "ltr"]> &103Equal<[IncludeType, D], ["RL", "rtl"]> extends true104? {105transform?: SpecificFieldPartial<Pick<Transform<L, R>, D>, D>106}107: {108transform: Pick<Transform<L, R>, D>109}110type StrictIncludeType<111IncludeType extends "LR" | "RL",112D extends Direction,113L,114R,115> = D extends "both"116? {117transform: SpecificFieldPartial<118Transform<L, R>,119IncludeType extends "LR" ? "ltr" : "rtl"120>121}122: D extends Exclude<Direction, "both">123? StrictIncludeMap<IncludeType, D, L, R>124: never125type IntersectButNotEqualType<D extends Direction, L, R> = D extends "both"126? {127transform: Transform<L, R>128}129: D extends Exclude<Direction, "both">130? {131transform: Pick<Transform<L, R>, D>132}133: never134type NotIntersectType<D extends Direction, L, R> = IntersectButNotEqualType<135D,136L,137R138>139interface Transform<L, R> {140ltr: (left: L) => R141rtl: (right: R) => L142}143type TransformType<D extends Direction, L, R> =144Equal<L, R> extends true145? EqualType<D, L, R>146: IncludeButNotEqual<L, R> extends true147? StrictIncludeType<"LR", D, L, R>148: IncludeButNotEqual<R, L> extends true149? StrictIncludeType<"RL", D, L, R>150: IntersectButNotEqual<L, R> extends true151? IntersectButNotEqualType<D, L, R>152: NotIntersect<L, R> extends true153? NotIntersectType<D, L, R>154: never155export type SyncRefOptions<156L,157R,158D extends Direction,159> = ConfigurableFlushSync & {160/**161* Watch deeply162*163* @default false164*/165deep?: boolean166/**167* Sync values immediately168*169* @default true170*/171immediate?: boolean172/**173* Direction of syncing. Value will be redefined if you define syncConvertors174*175* @default 'both'176*/177direction?: D178} & TransformType<D, L, R>179/**180* Two-way refs synchronization.181* From the set theory perspective to restrict the option's type182* Check in the following order:183* 1. L = R184* 2. L ∩ R ≠∅185* 3. L ⊆ R186* 4. L ∩ R = ∅187*/188export declare function syncRef<L, R, D extends Direction = "both">(189left: Ref<L>,190right: Ref<R>,191...[options]: Equal<L, R> extends true192? [options?: SyncRefOptions<L, R, D>]193: [options: SyncRefOptions<L, R, D>]194): () => void195```196