Skip to content

Commit

Permalink
feat(reactivity): add component class props reactivity (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlmoravek authored Jun 14, 2024
1 parent e9030bd commit c0e429d
Showing 1 changed file with 40 additions and 39 deletions.
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

0 comments on commit c0e429d

Please sign in to comment.