if the current focus is in the */
+div[data-pc-section='header']:focus-visible a {
+ @apply text-on-dark;
+}
diff --git a/src/components/SideNav/SideNav.vue b/src/components/SideNav/SideNav.vue
index 0b2a378db..6a9ee00b8 100644
--- a/src/components/SideNav/SideNav.vue
+++ b/src/components/SideNav/SideNav.vue
@@ -12,7 +12,7 @@
diff --git a/src/composables/useSideMenu.js b/src/composables/useSideMenu.js
index 351ba490a..858ebf738 100644
--- a/src/composables/useSideMenu.js
+++ b/src/composables/useSideMenu.js
@@ -1,6 +1,10 @@
import {toRef, ref, computed} from 'vue';
-export function useSideMenu(_items, _activeItemKey = '', _expandedKeys = {}) {
+export function useSideMenu(_items, opts = {}) {
+ const _activeItemKey = opts.activeItemKey || '';
+ const _expandedKeys = opts.expandedKeys || {};
+ const onActionFn = opts.onActionFn || (() => {});
+
const itemsRef = toRef(_items);
if (typeof itemsRef.value === 'undefined') {
throw new Error('items must be provided to use this api');
@@ -53,16 +57,31 @@ export function useSideMenu(_items, _activeItemKey = '', _expandedKeys = {}) {
function mapItems(_items, level = 1) {
const result = [];
- _items.forEach((_item) => {
+ _items.forEach((_item, index) => {
const item = {
..._item,
level,
+ index,
};
if (_item.items) {
item.items = mapItems(_item.items, level + 1);
}
+ if (item.link) {
+ item.command = () => {
+ window.location.href = item.link;
+ setActiveItemKey(item.key);
+ };
+ }
+
+ if (item.action) {
+ item.command = () => {
+ onActionFn(item.action, {...item.actionArgs, key: item.key});
+ setActiveItemKey(item.key);
+ };
+ }
+
result.push(item);
});
diff --git a/src/composables/useSideMenu.mdx b/src/composables/useSideMenu.mdx
index 0620fad37..7a39e0f33 100644
--- a/src/composables/useSideMenu.mdx
+++ b/src/composables/useSideMenu.mdx
@@ -4,7 +4,7 @@
## Usage
-To use the `useSideMenu` composable, first import it and then invoke it within your setup function. You need to pass the `items` array that represents your menu structure, and optionally pass an initial `activeItemKey`, an object of `expandedKeys`.
+To use the `useSideMenu` composable, first import it and then invoke it within your setup function. You need to pass the `items` array that represents your menu structure, and optionally pass an `opts` object. The `opts` object can include an initial `activeItemKey` and an `expandedKeys` object. If a menu item includes an `action` that should be triggered when clicked, you can pass this function within the `opts` object using the `onActionFn` attribute.
```javascript
const items = [
@@ -12,20 +12,49 @@ const items = [
label: 'Menu Item 1',
key: 'menuItem1',
items: [
- {label: 'Item 1.1', key: 'menuItem1_1'},
+ {
+ label: 'Item 1.1',
+ key: 'menuItem1_1',
+ action: 'openModal',
+ actionArgs: {},
+ },
{label: 'Item 1.2', key: 'menuItem1_2'},
],
},
{
label: 'Menu Item 2',
key: 'menuItem2',
- items: [{label: 'Item 2.1', key: 'menuItem2_1'}],
+ items: [
+ {
+ label: 'Item 2.1',
+ key: 'menuItem2_1',
+ action: 'openSideModal',
+ actionArgs: {},
+ },
+ ],
},
];
-const {activeItemKey, setActiveItemKey} = useSideMenu(items, 'menuItem1_1', {
- menuItem1: true,
- menuItem2: true,
+function handleActions(action, actionArgs) {
+ switch (action) {
+ case 'openModal':
+ openModal(actionArgs);
+ break;
+ case 'openSideModal':
+ openSideModal(actionArgs);
+ break;
+ default:
+ console.error(`No handler for action: ${action}`);
+ }
+}
+
+const {activeItemKey, setActiveItemKey} = useSideMenu(items, {
+ activeItemKey: 'menuItem1_1',
+ expandedKeys: {
+ menuItem1: true,
+ menuItem2: true,
+ },
+ onActionFn: handleActions,
});
```
@@ -76,9 +105,12 @@ const {selectedItem} = useSideMenu(items, 'menuItem2_1'); // will return {label:
This function allows you to set a new active item key dynamically from within your component or from other components that consume the `useSideMenu` composable. It accepts a string that represents the key of the item you want to set as active. This value will be assigned to the `activeItemKey` ref.
```javascript
-const {activeItemKey, setActiveItemKey} = useSideMenu(items, 'menuItem1_1', {
- menuItem1: true,
- menuItem2: true,
+const {activeItemKey, setActiveItemKey} = useSideMenu(items, {
+ activeItemKey: 'menuItem1_1',
+ expandedKeys: {
+ menuItem1: true,
+ menuItem2: true,
+ },
});
function someEvent() {
@@ -91,9 +123,12 @@ function someEvent() {
It allows you to dynamically update new set of keys that should be expanded in the `SideMenu` component. It accepts an array of strings where each string represents a key that should be expanded in the side menu.
```javascript
-const {expandedKeys, setExpandedKeys} = useSideMenu(items, 'menuItem1_1', {
- menuItem1: true,
- menuItem2: true,
+const {expandedKeys, setExpandedKeys} = useSideMenu(items, {
+ activeItemKey: 'menuItem1_1',
+ expandedKeys: {
+ menuItem1: true,
+ menuItem2: true,
+ },
});
function someEvent() {