index.mjs 958 B

1234567891011121314151617181920212223
  1. import { debugWarn } from "../../utils/error.mjs";
  2. import { fromPairs } from "lodash-unified";
  3. import { computed, getCurrentInstance } from "vue";
  4. //#region ../../packages/hooks/use-attrs/index.ts
  5. const DEFAULT_EXCLUDE_KEYS = ["class", "style"];
  6. const LISTENER_PREFIX = /^on[A-Z]/;
  7. const useAttrs = (params = {}) => {
  8. const { excludeListeners = false, excludeKeys } = params;
  9. const allExcludeKeys = computed(() => {
  10. return (excludeKeys?.value || []).concat(DEFAULT_EXCLUDE_KEYS);
  11. });
  12. const instance = getCurrentInstance();
  13. if (!instance) {
  14. debugWarn("use-attrs", "getCurrentInstance() returned null. useAttrs() must be called at the top of a setup function");
  15. return computed(() => ({}));
  16. }
  17. return computed(() => fromPairs(Object.entries(instance.proxy?.$attrs).filter(([key]) => !allExcludeKeys.value.includes(key) && !(excludeListeners && LISTENER_PREFIX.test(key)))));
  18. };
  19. //#endregion
  20. export { useAttrs };
  21. //# sourceMappingURL=index.mjs.map