useAttrs.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 (!allExcludeKeys.includes(key) && !(excludeListeners && LISTENER_PREFIX.test(key))) {
  23. acm[key] = val;
  24. }
  25. return acm;
  26. }, {} as Hash<any>);
  27. attrs.value = res;
  28. });
  29. return attrs;
  30. }