useAttrs.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { getCurrentInstance, reactive, shallowRef, watchEffect } from "vue";
  2. import type { Ref } from "vue";
  3. interface Params {
  4. excludeListeners?: boolean;
  5. excludeKeys?: string[];
  6. }
  7. const DEFAULT_EXCLUDE_KEYS = ["class", "style"];
  8. const LISTENER_PREFIX = /^on[A-Z]/;
  9. export function entries<T>(obj: Recordable<T>): [string, T][] {
  10. return Object.keys(obj).map((key: string) => [key, obj[key]]);
  11. }
  12. export function useAttrs(params: Params = {}): Ref<Recordable> | {} {
  13. const instance = getCurrentInstance();
  14. if (!instance) return {};
  15. const { excludeListeners = false, excludeKeys = [] } = params;
  16. const attrs = shallowRef({});
  17. const allExcludeKeys = excludeKeys.concat(DEFAULT_EXCLUDE_KEYS);
  18. // Since attrs are not reactive, make it reactive instead of doing in `onUpdated` hook for better performance
  19. instance.attrs = reactive(instance.attrs);
  20. watchEffect(() => {
  21. const res = entries(instance.attrs).reduce((acm, [key, val]) => {
  22. if (
  23. !allExcludeKeys.includes(key) &&
  24. !(excludeListeners && LISTENER_PREFIX.test(key))
  25. ) {
  26. acm[key] = val;
  27. }
  28. return acm;
  29. }, {} as Recordable);
  30. attrs.value = res;
  31. });
  32. return attrs;
  33. }