Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reactivity): add component class props reactivity #960

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions packages/oruga/src/composables/defineClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,46 +79,47 @@ export function defineClasses(
return { [computedClass]: applied };
}

// if suffix is defined, watch suffix changed and recalculate class
if (isDefined(suffix) && isRef(suffix)) {
scope.run(() => {
watch(
() => toValue(suffix),
(value, oldValue) => {
// only recompute when value has really changed
if (value === oldValue) return;
// recompute the class bind property
const classBind = getClassBind();
// update class binding property by class index
classes.value[index] = classBind;
},
);
});
}
// run all watcher and computed properties in an active effect scope
scope.run(() => {
// recompute the class bind property when the class property change
watch(
() => vm.proxy.$props[className],
() => {
// recompute the class bind property
const classBind = getClassBind();
// update class binding property by class index
classes.value[index] = classBind;
},
);

// if apply is defined, watch apply changed and update apply state (no need of recalculation here)
if (isDefined(apply) && isRef(apply)) {
scope.run(() => {
watch(
() => toValue(apply),
(applied, oldValue) => {
// only change apply when value has really changed
if (applied === oldValue) return;

// get class binding property by class index
const classBind = classes.value[index];

// update the apply class binding state
Object.keys(classBind).forEach(
(key) => (classBind[key] = applied),
);

// update the class binding property by class index
classes.value[index] = classBind;
},
);
});
}
// if suffix is defined, watch suffix changed and recalculate class
if (isDefined(suffix) && isRef(suffix)) {
watch(suffix, (value, oldValue) => {
// only recompute when value has really changed
if (value === oldValue) return;
// recompute the class bind property
const classBind = getClassBind();
// update class binding property by class index
classes.value[index] = classBind;
});
}

// if apply is defined, watch apply changed and update apply state (no need of recalculation here)
if (isDefined(apply) && isRef(apply)) {
watch(apply, (applied, oldValue) => {
// only change apply when value has really changed
if (applied === oldValue) return;
// get class binding property by class index
const classBind = classes.value[index];
// update the apply class binding state
Object.keys(classBind).forEach(
(key) => (classBind[key] = applied),
);
// update the class binding property by class index
classes.value[index] = classBind;
});
}
});

// return computed class based on parameter
return getClassBind();
Expand Down
Loading