Skip to content

Commit

Permalink
Remove the need to provide dropdownCollection
Browse files Browse the repository at this point in the history
We simplify the component interactions and rely on the `dropdownContext` instead

Signed-off-by: Phillip Rak <[email protected]>
  • Loading branch information
rak-phillip committed Jan 29, 2025
1 parent cdcac9d commit 14bb73d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 31 deletions.
12 changes: 2 additions & 10 deletions pkg/rancher-components/src/components/RcDropdown/RcDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,19 @@
import { useTemplateRef } from 'vue';
import { useClickOutside } from '@shell/composables/useClickOutside';
import { useDropdownContext } from '@components/RcDropdown/useDropdownContext';
import { useDropdownCollection } from '@components/RcDropdown/useDropdownCollection';
defineProps<{
ariaLabel?: string
}>();
const {
firstDropdownItem,
provideDropdownCollection,
registerDropdownCollection
} = useDropdownCollection();
provideDropdownCollection();
const {
isMenuOpen,
showMenu,
returnFocus,
setFocus,
provideDropdownContext,
} = useDropdownContext(firstDropdownItem);
registerDropdownCollection,
} = useDropdownContext();
provideDropdownContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script setup lang="ts">
import { inject } from 'vue';
import { DropdownCollection, defaultCollection, DropdownContext, defaultContext } from './types';
import { DropdownContext, defaultContext } from './types';
const props = defineProps({ disabled: Boolean });
const emits = defineEmits(['click']);
const { dropdownItems } = inject<DropdownCollection>('dropdownCollection') || defaultCollection;
const { close } = inject<DropdownContext>('dropdownContext') || defaultContext;
const { close, dropdownItems } = inject<DropdownContext>('dropdownContext') || defaultContext;
/**
* Handles keydown events to navigate between dropdown items.
Expand Down
8 changes: 2 additions & 6 deletions pkg/rancher-components/src/components/RcDropdown/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type DropdownContext = {
handleKeydown: () => void;
showMenu: (show: boolean) => void;
registerTrigger: (triggerRef: RcButtonType | null) => void;
dropdownItems: Ref<Element[]>;
focusFirstElement: () => void;
isMenuOpen: Ref<boolean>;
close: () => void;
Expand All @@ -14,13 +15,8 @@ export const defaultContext: DropdownContext = {
handleKeydown: () => null,
showMenu: (_show: boolean | null) => null,
registerTrigger: (_triggerRef: RcButtonType | null) => null,
dropdownItems: ref([]),
focusFirstElement: () => null,
isMenuOpen: ref(false),
close: () => null,
};

export type DropdownCollection = {
dropdownItems: Ref<Element[]>;
};

export const defaultCollection: DropdownCollection = { dropdownItems: ref([]) };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { provide, ref } from 'vue';
import { ref } from 'vue';

/**
* Manages a collection of dropdown items. Includes methods for registering
Expand Down Expand Up @@ -37,17 +37,9 @@ export const useDropdownCollection = () => {
});
};

/**
* Provides the dropdown collection data to descendants. Can be accessed
* in descendants with the `inject()` function.
*/
const provideDropdownCollection = () => {
provide('dropdownCollection', { dropdownItems });
};

return {
dropdownItems,
firstDropdownItem,
provideDropdownCollection,
registerDropdownCollection,
};
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ref, provide, Ref, nextTick } from 'vue';
import { ref, provide, nextTick } from 'vue';
import { useDropdownCollection } from './useDropdownCollection';
import { RcButtonType } from '@components/RcButton';

/**
Expand All @@ -10,7 +11,8 @@ import { RcButtonType } from '@components/RcButton';
* @returns Dropdown context methods and state. Used for programmatic
* interactions and setting focus.
*/
export const useDropdownContext = (firstDropdownItem: Ref<HTMLElement | null>) => {
export const useDropdownContext = () => {
const { dropdownItems, firstDropdownItem, registerDropdownCollection } = useDropdownCollection();
/**
* Tracks if a keydown event has occurred. Important for distinguishing keyboard
* events from mouse events.
Expand Down Expand Up @@ -78,6 +80,7 @@ export const useDropdownContext = (firstDropdownItem: Ref<HTMLElement | null>) =
showMenu,
registerTrigger,
isMenuOpen,
dropdownItems,
close: () => returnFocus(),
focusFirstElement: () => {
handleKeydown();
Expand All @@ -93,5 +96,6 @@ export const useDropdownContext = (firstDropdownItem: Ref<HTMLElement | null>) =
returnFocus,
setFocus,
provideDropdownContext,
registerDropdownCollection,
};
};

0 comments on commit 14bb73d

Please sign in to comment.