Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(OnyxNavButton): prop mobileChildrenOpen not working #1977

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-frogs-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sit-onyx": patch
---

fix(OnyxNavButton): prop `mobileChildrenOpen` not working
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@
// this layout component is only used internally for the nav button component
// to easily switch between mobile and desktop layout
import arrowSmallLeft from "@sit-onyx/icons/arrow-small-left.svg?raw";
import { toRef } from "vue";
import { MANAGED_SYMBOL, useManagedState } from "../../../../composables/useManagedState";
import { injectI18n } from "../../../../i18n";
import OnyxButton from "../../../OnyxButton/OnyxButton.vue";
import OnyxFlyoutMenu from "../OnyxFlyoutMenu/OnyxFlyoutMenu.vue";
import OnyxNavSeparator from "../OnyxNavSeparator/OnyxNavSeparator.vue";
import type { OnyxNavButtonProps } from "./types";

const props = withDefaults(
defineProps<
OnyxNavButtonProps & {
/**
* If the mobile layout should be used.
*/
isMobile: boolean;
}
>(),
{
mobileChildrenOpen: MANAGED_SYMBOL,
},
);
const props = defineProps<
OnyxNavButtonProps & {
/**
* If the mobile layout should be used.
*/
isMobile: boolean;
}
>();

const slots = defineSlots<{
button?(): unknown;
Expand All @@ -34,12 +27,6 @@ const { t } = injectI18n();
const emit = defineEmits<{
"update:mobileChildrenOpen": [isOpen: boolean];
}>();

const { state: mobileChildrenOpen } = useManagedState(
toRef(() => props.mobileChildrenOpen),
false,
(newVal) => emit("update:mobileChildrenOpen", newVal),
);
</script>

<template>
Expand All @@ -51,7 +38,7 @@ const { state: mobileChildrenOpen } = useManagedState(
mode="plain"
color="neutral"
:icon="arrowSmallLeft"
@click="mobileChildrenOpen = false"
@click="emit('update:mobileChildrenOpen', false)"
/>

<slot v-if="!mobileChildrenOpen || props.href" name="button"></slot>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts" setup>
import chevronRightSmall from "@sit-onyx/icons/chevron-right-small.svg?raw";
import { computed, inject, ref } from "vue";
import { computed, inject, toRef } from "vue";
import { MANAGED_SYMBOL, useManagedState } from "../../../../composables/useManagedState";
import OnyxExternalLinkIcon from "../../../OnyxExternalLinkIcon/OnyxExternalLinkIcon.vue";
import OnyxIcon from "../../../OnyxIcon/OnyxIcon.vue";
import { MOBILE_NAV_BAR_INJECTION_KEY } from "../../types";
Expand All @@ -10,13 +11,18 @@ import type { OnyxNavButtonProps } from "./types";
const props = withDefaults(defineProps<OnyxNavButtonProps>(), {
active: false,
withExternalIcon: "auto",
mobileChildrenOpen: MANAGED_SYMBOL,
});

const emit = defineEmits<{
/**
* Emitted when the nav button is clicked (via click or keyboard).
*/
navigate: [href: string, event: MouseEvent];
/**
* Emitted when the mobile children are open or closed.
*/
"update:mobileChildrenOpen": [isOpen: boolean];
}>();

const slots = defineSlots<{
Expand All @@ -31,12 +37,17 @@ const slots = defineSlots<{
}>();

const isMobile = inject(MOBILE_NAV_BAR_INJECTION_KEY);
const isMobileChildrenOpen = ref(false);
const hasChildren = computed(() => !!slots.children);

const { state: mobileChildrenOpen } = useManagedState(
toRef(() => props.mobileChildrenOpen),
false,
(newVal) => emit("update:mobileChildrenOpen", newVal),
);

const handleParentClick = (event: MouseEvent) => {
if (isMobile?.value && hasChildren.value && !isMobileChildrenOpen.value) {
isMobileChildrenOpen.value = true;
if (isMobile?.value && hasChildren.value && !mobileChildrenOpen.value) {
mobileChildrenOpen.value = true;
} else if (props.href) {
emit("navigate", props.href, event);
}
Expand All @@ -46,7 +57,7 @@ const handleParentClick = (event: MouseEvent) => {
<template>
<NavButtonLayout
v-bind="props"
v-model:mobile-children-open="isMobileChildrenOpen"
v-model:mobile-children-open="mobileChildrenOpen"
class="onyx-nav-button"
:class="{
'onyx-nav-button--mobile': isMobile,
Expand All @@ -69,7 +80,7 @@ const handleParentClick = (event: MouseEvent) => {
</slot>

<OnyxIcon
v-if="isMobile && hasChildren && !isMobileChildrenOpen"
v-if="isMobile && hasChildren && !mobileChildrenOpen"
class="onyx-nav-button__mobile-chevron"
:icon="chevronRightSmall"
/>
Expand Down
Loading