Skip to content

Commit

Permalink
feat: make internal demo app functional (#1574)
Browse files Browse the repository at this point in the history
BREAKING-CHANGE: refactors the GlobalXBus to use a prop to receive the listeners instead of relying on attributes.
  • Loading branch information
diegopf authored Jul 24, 2024
1 parent c6b28e5 commit a485dea
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 314 deletions.
20 changes: 14 additions & 6 deletions packages/x-components/src/components/base-grid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@
defineComponent,
inject,
onBeforeUnmount,
onMounted,
PropType,
Ref,
ref,
watch
} from 'vue';
import { MaybeComputedElementRef, useResizeObserver } from '@vueuse/core';
import {
MaybeComputedElementRef,
useResizeObserver,
UseResizeObserverReturn
} from '@vueuse/core';
import { toKebabCase } from '../utils/string';
import { ListItem, VueCSSClasses } from '../utils/types';
import { AnimationProp } from '../types/animation-prop';
Expand Down Expand Up @@ -229,11 +234,14 @@
*
* @internal
*/
const resizeObserver = useResizeObserver(
gridEl as MaybeComputedElementRef,
updateRenderedColumnsNumber
);
onBeforeUnmount(() => resizeObserver.stop());
let resizeObserver: UseResizeObserverReturn;
onMounted(() => {
resizeObserver = useResizeObserver(
gridEl as MaybeComputedElementRef,
updateRenderedColumnsNumber
);
});
onBeforeUnmount(() => resizeObserver?.stop());
return {
gridItems,
Expand Down
19 changes: 12 additions & 7 deletions packages/x-components/src/components/global-x-bus.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { defineComponent, PropType } from 'vue';
import { XEventListeners } from '../x-installer/api/api.types';
import { XEvent } from '../wiring/events.types';
import { useNoElementRender } from '../composables/use-no-element-render';
import { useXBus } from '../composables/use-x-bus';
/**
Expand All @@ -13,20 +12,26 @@
*/
export default defineComponent({
name: 'GlobalXBus',
setup(_, { listeners, slots }) {
props: {
listeners: {
type: Object as PropType<XEventListeners>,
required: true
}
},
setup(props) {
const xBus = useXBus();
/**
* Handles a subscription to all the events provided in the listeners with the function that
* will execute the callback.
*/
Object.entries(listeners as XEventListeners).forEach(([eventName, callback]) => {
Object.entries(props.listeners as XEventListeners).forEach(([eventName, callback]) => {
xBus.on(eventName as XEvent, true).subscribe(({ eventPayload, metadata }) => {
callback(eventPayload as never, metadata);
});
});
return () => useNoElementRender(slots);
return () => '';
}
});
</script>
Expand All @@ -39,11 +44,11 @@ This component emits no own events, but you can subscribe to any X Event using V
## See it in action

This component does not render anything. Its only responsibility is to facilitate listening to any X
Event by using Vue component listeners.
Event by using the prop `listeners`

```vue
<template>
<GlobalXBus @UserAcceptedAQuery="printQuery" />
<GlobalXBus :listeners="{ UserAcceptedAQuery: printQuery }" />
</template>

<script>
Expand Down
2 changes: 1 addition & 1 deletion packages/x-components/src/components/snippet-callbacks.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<GlobalXBus v-on="eventListeners" />
<GlobalXBus :listeners="eventListeners" />
</template>

<script lang="ts">
Expand Down
Loading

0 comments on commit a485dea

Please sign in to comment.