Skip to content
Open
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
24 changes: 10 additions & 14 deletions resources/js/components/AppHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import {
TooltipTrigger,
} from '@/components/ui/tooltip';
import UserMenuContent from '@/components/UserMenuContent.vue';
import { useActiveUrl } from '@/composables/useActiveUrl';
import { getInitials } from '@/composables/useInitials';
import { toUrl, urlIsActive } from '@/lib/utils';
import { dashboard } from '@/routes';
import type { BreadcrumbItem, NavItem } from '@/types';
import { InertiaLinkProps, Link, usePage } from '@inertiajs/vue3';
import type { InertiaLinkProps } from '@inertiajs/vue3';
import { Link, usePage } from '@inertiajs/vue3';
import { BookOpen, Folder, LayoutGrid, Menu, Search } from 'lucide-vue-next';
import { computed } from 'vue';

Expand All @@ -47,18 +48,13 @@ const props = withDefaults(defineProps<Props>(), {

const page = usePage();
const auth = computed(() => page.props.auth);
const { urlIsActive, toUrl } = useActiveUrl();

const isCurrentRoute = computed(
() => (url: NonNullable<InertiaLinkProps['href']>) =>
urlIsActive(url, page.url),
);

const activeItemStyles = computed(
() => (url: NonNullable<InertiaLinkProps['href']>) =>
isCurrentRoute.value(toUrl(url))
? 'text-neutral-900 dark:bg-neutral-800 dark:text-neutral-100'
: '',
);
function activeItemStyles(url: NonNullable<InertiaLinkProps['href']>) {
return urlIsActive(url)
? 'text-neutral-900 dark:bg-neutral-800 dark:text-neutral-100'
: '';
}

const mainNavItems: NavItem[] = [
{
Expand Down Expand Up @@ -179,7 +175,7 @@ const rightNavItems: NavItem[] = [
{{ item.title }}
</Link>
<div
v-if="isCurrentRoute(item.href)"
v-if="urlIsActive(item.href)"
class="absolute bottom-0 left-0 h-0.5 w-full translate-y-px bg-black dark:bg-white"
></div>
</NavigationMenuItem>
Expand Down
4 changes: 3 additions & 1 deletion resources/js/components/NavFooter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
SidebarMenuButton,
SidebarMenuItem,
} from '@/components/ui/sidebar';
import { toUrl } from '@/lib/utils';
import { useActiveUrl } from '@/composables/useActiveUrl';
import { type NavItem } from '@/types';

interface Props {
Expand All @@ -15,6 +15,8 @@ interface Props {
}

defineProps<Props>();

const { toUrl } = useActiveUrl();
</script>

<template>
Expand Down
8 changes: 4 additions & 4 deletions resources/js/components/NavMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
SidebarMenuButton,
SidebarMenuItem,
} from '@/components/ui/sidebar';
import { urlIsActive } from '@/lib/utils';
import { useActiveUrl } from '@/composables/useActiveUrl';
import { type NavItem } from '@/types';
import { Link, usePage } from '@inertiajs/vue3';
import { Link } from '@inertiajs/vue3';

defineProps<{
items: NavItem[];
}>();

const page = usePage();
const { urlIsActive } = useActiveUrl();
</script>

<template>
Expand All @@ -24,7 +24,7 @@ const page = usePage();
<SidebarMenuItem v-for="item in items" :key="item.title">
<SidebarMenuButton
as-child
:is-active="urlIsActive(item.href, page.url)"
:is-active="urlIsActive(item.href)"
:tooltip="item.title"
>
<Link :href="item.href">
Expand Down
28 changes: 28 additions & 0 deletions resources/js/composables/useActiveUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { InertiaLinkProps } from '@inertiajs/vue3';
import { usePage } from '@inertiajs/vue3';
import { computed, readonly } from 'vue';

const page = usePage();
const currentUrlReactive = computed(
() => new URL(page.url, window?.location.origin).pathname,
);

function toUrl(href: NonNullable<InertiaLinkProps['href']>) {
return typeof href === 'string' ? href : href?.url;
}

export function useActiveUrl() {
function urlIsActive(
urlToCheck: NonNullable<InertiaLinkProps['href']>,
currentUrl?: string,
) {
const urlToCompare = currentUrl ?? currentUrlReactive.value;
return toUrl(urlToCheck) === urlToCompare;
}

return {
currentUrl: readonly(currentUrlReactive),
urlIsActive,
toUrl,
};
}
6 changes: 3 additions & 3 deletions resources/js/layouts/settings/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Heading from '@/components/Heading.vue';
import { Button } from '@/components/ui/button';
import { Separator } from '@/components/ui/separator';
import { toUrl, urlIsActive } from '@/lib/utils';
import { useActiveUrl } from '@/composables/useActiveUrl';
import { edit as editAppearance } from '@/routes/appearance';
import { edit as editProfile } from '@/routes/profile';
import { show } from '@/routes/two-factor';
Expand All @@ -29,7 +29,7 @@ const sidebarNavItems: NavItem[] = [
},
];

const currentPath = typeof window !== undefined ? window.location.pathname : '';
const { toUrl, urlIsActive } = useActiveUrl();
</script>

<template>
Expand All @@ -48,7 +48,7 @@ const currentPath = typeof window !== undefined ? window.location.pathname : '';
variant="ghost"
:class="[
'w-full justify-start',
{ 'bg-muted': urlIsActive(item.href, currentPath) },
{ 'bg-muted': urlIsActive(item.href) },
]"
as-child
>
Expand Down
12 changes: 0 additions & 12 deletions resources/js/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import { InertiaLinkProps } from '@inertiajs/vue3';
import { clsx, type ClassValue } from 'clsx';
import { twMerge } from 'tailwind-merge';

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

export function urlIsActive(
urlToCheck: NonNullable<InertiaLinkProps['href']>,
currentUrl: string,
) {
return toUrl(urlToCheck) === currentUrl;
}

export function toUrl(href: NonNullable<InertiaLinkProps['href']>) {
return typeof href === 'string' ? href : href?.url;
}