Tabs

AkarGitHub
A set of tab panels that are displayed one at a time.

Usage

Use the Tabs component to display a list of items in a tabs.

<script setup lang="ts">
import { reactive } from 'vue'

const items = [
  {
    label: 'Account',
    icon: 'i-lucide:user',
    slot: 'account'
  },
  {
    label: 'Password',
    icon: 'i-lucide:lock',
    slot: 'password'
  }
]

const state = reactive({
  name: 'praburangki',
  username: 'praburangki',
  currentPassword: '',
  newPassword: '',
  confirmPassword: ''
})
</script>

<template>
  <PTabs :items="items">
    <template #account>
      <PForm :state="state" class="flex flex-col gap-4">
        <PFormField label="Name" name="name">
          <PInput v-model="state.name" class="w-full" />
        </PFormField>
        <PFormField label="Username" name="username">
          <PInput v-model="state.username" class="w-full" />
        </PFormField>
      </PForm>
    </template>

    <template #password>
      <PForm :state="state" class="flex flex-col gap-4">
        <PFormField label="Current Password" name="current" required>
          <PInput v-model="state.currentPassword" type="password" required class="w-full" />
        </PFormField>
        <PFormField label="New Password" name="new" required>
          <PInput v-model="state.newPassword" type="password" required class="w-full" />
        </PFormField>
        <PFormField label="Confirm Password" name="confirm" required>
          <PInput v-model="state.confirmPassword" type="password" required class="w-full" />
        </PFormField>
      </PForm>
    </template>
  </PTabs>
</template>

Items

Use the items prop as an array of objects with the following properties:

  • label?: string
  • icon?: string
  • avatar?: AvatarProps
  • badge?: string | number | BadgeProps
  • content?: string
  • value?: string | number
  • disabled?: boolean
  • slot?: string
  • class?: any
  • pohon?: { trigger?: ClassNameValue, leadingIcon?: ClassNameValue, leadingAvatar?: ClassNameValue, leadingAvatarSize?: ClassNameValue, label?: ClassNameValue, trailingBadge?: ClassNameValue, trailingBadgeSize?: ClassNameValue, content?: ClassNameValue }
This is the account content.
<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui'


const items = ref<PTabsItem[]>([
  {
    label: 'Account',
    icon: 'i-lucide:user',
    content: 'This is the account content.'
  },
  {
    label: 'Password',
    icon: 'i-lucide:lock',
    content: 'This is the password content.'
  }
])
</script>

<template>
  <PTabs :items="items" class="w-full" />
</template>

Content

Set the content prop to false to turn the Tabs into a toggle-only control without displaying any content. Defaults to true.

<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui'


const items = ref<PTabsItem[]>([
  {
    label: 'Account',
    icon: 'i-lucide:user',
    content: 'This is the account content.'
  },
  {
    label: 'Password',
    icon: 'i-lucide:lock',
    content: 'This is the password content.'
  }
])
</script>

<template>
  <PTabs :items="items" class="w-full" />
</template>

Unmount

Use the unmount-on-hide prop to prevent the content from being unmounted when the Tabs is collapsed. Defaults to true.

This is the account content.
<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui'


const items = ref<PTabsItem[]>([
  {
    label: 'Account',
    icon: 'i-lucide:user',
    content: 'This is the account content.'
  },
  {
    label: 'Password',
    icon: 'i-lucide:lock',
    content: 'This is the password content.'
  }
])
</script>

<template>
  <PTabs :items="items" class="w-full" />
</template>
You can inspect the DOM to see each item's content being rendered.

Color

Use the color prop to change the color of the Tabs.

<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui'


const items = ref<PTabsItem[]>([
  {
    label: 'Account'
  },
  {
    label: 'Password'
  }
])
</script>

<template>
  <PTabs color="neutral" :items="items" class="w-full" />
</template>

Variant

Use the variant prop to change the variant of the Tabs.

<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui'


const items = ref<PTabsItem[]>([
  {
    label: 'Account'
  },
  {
    label: 'Password'
  }
])
</script>

<template>
  <PTabs color="neutral" variant="link" :items="items" class="w-full" />
</template>

Size

Use the size prop to change the size of the Tabs.

<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui'


const items = ref<PTabsItem[]>([
  {
    label: 'Account'
  },
  {
    label: 'Password'
  }
])
</script>

<template>
  <PTabs size="md" variant="pill" :items="items" class="w-full" />
</template>

Orientation

Use the orientation prop to change the orientation of the Tabs. Defaults to horizontal.

<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui'


const items = ref<PTabsItem[]>([
  {
    label: 'Account'
  },
  {
    label: 'Password'
  }
])
</script>

<template>
  <PTabs orientation="vertical" variant="pill" :items="items" class="w-full" />
</template>

Examples

Control active item

You can control the active item by using the default-value prop or the v-model directive with the index of the item.

<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui';
import { useRoute, useRouter } from '#app';
import { computed } from 'vue';

const route = useRoute();
const router = useRouter();

const items: Array<PTabsItem> = [
  {
    label: 'Account',
    value: 'account',
  },
  {
    label: 'Password',
    value: 'password',
  },
];

const active = computed({
  get() {
    return (route.query.tab as string) || 'account';
  },
  set(tab) {
    // Hash is specified here to prevent the page from scrolling to the top
    router.push({
      path: '/docs/pohon/components/tabs',
      query: { tab },
      hash: '#control-active-item',
    });
  },
});
</script>

<template>
  <PTabs
    v-model="active"
    :content="false"
    :items="items"
    class="w-full"
  />
</template>

With content slot

Use the #content slot to customize the content of each item.

This is the Account tab.

<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui';

const items: Array<PTabsItem> = [
  {
    label: 'Account',
    icon: 'i-lucide:user',
  },
  {
    label: 'Password',
    icon: 'i-lucide:lock',
  },
];
</script>

<template>
  <PTabs
    :items="items"
    class="w-full"
  >
    <template #content="{ item }">
      <p>This is the {{ item.label }} tab.</p>
    </template>
  </PTabs>
</template>

With custom slot

Use the slot property to customize a specific item.

You will have access to the following slots:

  • #{{ item.slot }}

Make changes to your account here. Click save when you're done.

<script setup lang="ts">
import type { PTabsItem } from 'pohon-ui';
import { reactive } from 'vue';

const items = [
  {
    label: 'Account',
    description: 'Make changes to your account here. Click save when you\'re done.',
    icon: 'i-lucide:user',
    slot: 'account' as const,
  },
  {
    label: 'Password',
    description: 'Change your password here. After saving, you\'ll be logged out.',
    icon: 'i-lucide:lock',
    slot: 'password' as const,
  },
] satisfies Array<PTabsItem>;

const state = reactive({
  name: 'praburangki',
  username: 'praburangki',
  currentPassword: '',
  newPassword: '',
  confirmPassword: '',
});
</script>

<template>
  <PTabs
    :items="items"
    variant="link"
    :pohon="{ trigger: 'grow' }"
    class="gap-4 w-full"
  >
    <template #account="{ item }">
      <p class="color-text-muted mb-4">
        {{ item.description }}
      </p>

      <PForm
        :state="state"
        class="flex flex-col gap-4"
      >
        <PFormField
          label="Name"
          name="name"
        >
          <PInput
            v-model="state.name"
            class="w-full"
          />
        </PFormField>
        <PFormField
          label="Username"
          name="username"
        >
          <PInput
            v-model="state.username"
            class="w-full"
          />
        </PFormField>

        <PButton
          label="Save changes"
          type="submit"
          variant="soft"
          class="self-end"
        />
      </PForm>
    </template>

    <template #password="{ item }">
      <p class="color-text-muted mb-4">
        {{ item.description }}
      </p>

      <PForm
        :state="state"
        class="flex flex-col gap-4"
      >
        <PFormField
          label="Current Password"
          name="current"
          required
        >
          <PInput
            v-model="state.currentPassword"
            type="password"
            required
            class="w-full"
          />
        </PFormField>
        <PFormField
          label="New Password"
          name="new"
          required
        >
          <PInput
            v-model="state.newPassword"
            type="password"
            required
            class="w-full"
          />
        </PFormField>
        <PFormField
          label="Confirm Password"
          name="confirm"
          required
        >
          <PInput
            v-model="state.confirmPassword"
            type="password"
            required
            class="w-full"
          />
        </PFormField>

        <PButton
          label="Change password"
          type="submit"
          variant="soft"
          class="self-end"
        />
      </PForm>
    </template>
  </PTabs>
</template>

API

Props

Prop Default Type

Slots

Slot Type

Emits

Event Type

Expose

When accessing the component via a template ref, you can use the following:

NameType
triggersRefRef<ComponentPublicInstance[]>

Theme

We use unocss-variants to customize the theme. Read more about it in the theming guide.

Below is the theme configuration skeleton for the PTabs. Since the component is provided unstyled by default, you will need to fill in these values to apply your own custom look and feel. If you prefer to use our pre-built, opinionated styling, you can instead use our UnoCSS preset, this docs is using it as well.

app.config.ts
export default defineAppConfig({
  pohon: {
    tabs: {
      slots: {
        root: '',
        list: '',
        indicator: '',
        trigger: '',
        leadingIcon: '',
        leadingAvatar: '',
        leadingAvatarSize: '',
        label: '',
        trailingBadge: '',
        trailingBadgeSize: '',
        content: ''
      },
      variants: {
        color: {
          primary: '',
          secondary: '',
          success: '',
          info: '',
          warning: '',
          error: '',
          neutral: ''
        },
        variant: {
          pill: {
            list: '',
            trigger: '',
            indicator: ''
          },
          link: {
            list: '',
            indicator: '',
            trigger: ''
          }
        },
        orientation: {
          horizontal: {
            root: '',
            list: '',
            indicator: '',
            trigger: ''
          },
          vertical: {
            list: '',
            indicator: ''
          }
        },
        size: {
          xs: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          },
          sm: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          },
          md: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          },
          lg: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          },
          xl: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          }
        }
      },
      compoundVariants: [],
      defaultVariants: {
        color: 'primary',
        variant: 'pill',
        size: 'md'
      }
    }
  }
};
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import pohon from 'pohon-ui/vite'

export default defineAppConfig({
  pohon: {
    tabs: {
      slots: {
        root: '',
        list: '',
        indicator: '',
        trigger: '',
        leadingIcon: '',
        leadingAvatar: '',
        leadingAvatarSize: '',
        label: '',
        trailingBadge: '',
        trailingBadgeSize: '',
        content: ''
      },
      variants: {
        color: {
          primary: '',
          secondary: '',
          success: '',
          info: '',
          warning: '',
          error: '',
          neutral: ''
        },
        variant: {
          pill: {
            list: '',
            trigger: '',
            indicator: ''
          },
          link: {
            list: '',
            indicator: '',
            trigger: ''
          }
        },
        orientation: {
          horizontal: {
            root: '',
            list: '',
            indicator: '',
            trigger: ''
          },
          vertical: {
            list: '',
            indicator: ''
          }
        },
        size: {
          xs: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          },
          sm: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          },
          md: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          },
          lg: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          },
          xl: {
            trigger: '',
            leadingIcon: '',
            leadingAvatarSize: ''
          }
        }
      },
      compoundVariants: [],
      defaultVariants: {
        color: 'primary',
        variant: 'pill',
        size: 'md'
      }
    }
  }
};

Akar

With Pohon UI, you can achieve similar component functionality with less code and effort, as it comes with built-in styles mechanism and behaviors that are optimized for common use cases. Since it's using unocss-variants it adds a runtime cost, but it can be worth it if you prioritize development speed and ease of use over fine-grained control.

If this is a deal breaker for you, you can always stick to using Akar and build your own custom components on top of it.

Changelog

No recent changes