Skip to content

Commit

Permalink
feat(Divider): new component (#757)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
  • Loading branch information
Haythamasalama and benjamincanac committed Oct 5, 2023
1 parent 0c807db commit eb9ce6a
Show file tree
Hide file tree
Showing 6 changed files with 384 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<UDivider>
<Logo class="w-28 h-6" />
</UDivider>
</template>
47 changes: 47 additions & 0 deletions docs/components/content/examples/DividerExampleOrientation.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script setup>
const form = reactive({ email: 'mail@example.com', password: 'password' })
</script>

<template>
<div class="w-full flex flex-col gap-y-4">
<UCard :ui="{ body: { base: 'grid grid-cols-3' } }">
<div class="space-y-4">
<UFormGroup label="Email" name="email">
<UInput v-model="form.email" />
</UFormGroup>

<UFormGroup label="Password" name="password">
<UInput v-model="form.password" type="password" />
</UFormGroup>

<UButton label="Login" color="gray" block />
</div>

<UDivider label="OR" color="gray" orientation="vertical" />

<div class="space-y-4 flex flex-col justify-center">
<UButton color="black" label="Login with GitHub" icon="i-simple-icons-github" block />
<UButton color="black" label="Login with Google" icon="i-simple-icons-google" block />
</div>
</UCard>

<UCard>
<div class="space-y-4">
<UFormGroup label="Email" name="email">
<UInput v-model="form.email" />
</UFormGroup>

<UFormGroup label="Password" name="password">
<UInput v-model="form.password" type="password" />
</UFormGroup>

<UButton label="Login" color="gray" block />

<UDivider label="OR" color="gray" />

<UButton color="black" label="Login with GitHub" icon="i-simple-icons-github" block />
<UButton color="black" label="Login with Google" icon="i-simple-icons-google" block />
</div>
</UCard>
</div>
</template>
184 changes: 184 additions & 0 deletions docs/content/7.layout/4.divider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
---
description: Display a separator between content.
links:
- label: GitHub
icon: i-simple-icons-github
to: https://github.com/nuxt/ui/blob/dev/src/runtime/components/layout/Divider.vue
navigation:
badge: New
---

## Usage

You can pass `label`, `icon` or `avatar` to the divider component.

### Label

::component-card
---
props:
label: OR
---
::

### Icon

Use any icon from [Iconify](https://icones.js.org) by setting the `icon` prop by using this pattern: `i-{collection_name}-{icon_name}`.

::component-card
---
props:
icon: 'i-simple-icons-github'
excludedProps:
- icon
---
::

### Avatar

Use the [avatar](/elements/avatar) prop as an `object` and configure it with any of its props.

::component-card
---
props:
avatar:
src: https://avatars.githubusercontent.com/u/739984?v=4
excludedProps:
- avatar
---
::

### Orientation

You can change the orientation of the divider by setting the `orientation` prop to `horizontal` or `vertical`. Defaults to `horizontal`.

::component-example
#default
:divider-example-orientation

#code
```vue
<script setup>
const form = reactive({ email: 'mail@example.com', password: 'password' })
</script>
<template>
<div class="w-full flex flex-col gap-y-4">
<UCard :ui="{ body: { base: 'grid grid-cols-3' } }">
<div class="space-y-4">
<UFormGroup label="Email" name="email">
<UInput v-model="form.email" />
</UFormGroup>
<UFormGroup label="Password" name="password">
<UInput v-model="form.password" type="password" />
</UFormGroup>
<UButton label="Login" color="gray" block />
</div>
<UDivider label="OR" color="gray" orientation="vertical" />
<div class="space-y-4 flex flex-col justify-center">
<UButton color="black" label="Login with GitHub" icon="i-simple-icons-github" block />
<UButton color="black" label="Login with Google" icon="i-simple-icons-google" block />
</div>
</UCard>
<UCard>
<div class="space-y-4">
<UFormGroup label="Email" name="email">
<UInput v-model="form.email" />
</UFormGroup>
<UFormGroup label="Password" name="password">
<UInput v-model="form.password" type="password" />
</UFormGroup>
<UButton label="Login" color="gray" block />
<UDivider label="OR" color="gray" />
<UButton color="black" label="Login with GitHub" icon="i-simple-icons-github" block />
<UButton color="black" label="Login with Google" icon="i-simple-icons-google" block />
</div>
</UCard>
</div>
</template>
```
::

### Type

You can change the type of the divider by setting the `type` prop to `solid`, `dotted` or `dashed`. Defaults to `solid`.

::component-card{class="w-full"}
---
props:
label: Nuxt UI
type: dashed
excludedProps:
- label
---
::

### Size

You can change the size of the divider by using the `ui` prop

::component-card
---
props:
label: Nuxt UI
ui:
border:
size:
horizontal: border-t-2
excludedProps:
- label
- ui
---
::

### Color

You can change the color of the content by using the `ui` prop

::component-card
---
props:
label: Nuxt UI
ui:
label: text-primary-500 dark:text-primary-400
excludedProps:
- label
- ui
---
::

## Slots

### `default`

Use the `default` slot to add content to the divider.

::component-example
#default
:divider-example-default-slot
#code
```vue
<template>
<UDivider>
<Logo class="w-28 h-6" />
</UDivider>
</template>
```
::

## Props

:component-props

## Preset

:component-preset
4 changes: 2 additions & 2 deletions docs/pages/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<UPageBody prose>
<ContentRenderer v-if="page.body" :value="page" />

<UDivider v-if="surround?.length" />
<hr v-if="surround?.length">
<UDocsSurround :surround="(surround as ParsedContent[])" />
</UPageBody>
Expand All @@ -14,7 +14,7 @@
<UDocsToc :links="page.body.toc.links">
<template #bottom>
<div class="hidden lg:block space-y-6 !mt-6">
<UDivider v-if="page.body?.toc?.links?.length" dashed />
<UDivider v-if="page.body?.toc?.links?.length" type="dashed" />
<UPageLinks title="Community" :links="links" />
</div>
Expand Down
116 changes: 116 additions & 0 deletions src/runtime/components/layout/Divider.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<template>
<div :class="wrapperClass" v-bind="attrs">
<div :class="borderClass" />

<div v-if="label || icon || avatar || $slots.default" :class="containerClass">
<slot>
<span v-if="label" :class="ui.label">
{{ label }}
</span>
<UIcon v-else-if="icon" :name="icon" :class="ui.icon.base" />
<UAvatar v-else-if="avatar" v-bind="{ size: ui.avatar.size, ...avatar }" :class="ui.avatar.base" />
</slot>
</div>

<div :class="borderClass" />
</div>
</template>

<script lang="ts">
import { toRef, computed, defineComponent } from 'vue'
import type { PropType } from 'vue'
import { twMerge, twJoin } from 'tailwind-merge'
import UIcon from '../elements/Icon.vue'
import UAvatar from '../elements/Avatar.vue'
import { useUI } from '../../composables/useUI'
import { mergeConfig } from '../../utils'
import type { Avatar, Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
import { divider } from '#ui/ui.config'
const config = mergeConfig<typeof divider>(appConfig.ui.strategy, appConfig.ui.divider, divider)
export default defineComponent({
components: {
UIcon,
UAvatar
},
inheritAttrs: false,
props: {
label: {
type: String,
default: null
},
icon: {
type: String,
default: null
},
avatar: {
type: Object as PropType<Avatar>,
default: null
},
orientation: {
type: String as PropType<'horizontal' | 'vertical'>,
default: 'horizontal',
validator: (value: string) => ['horizontal', 'vertical'].includes(value)
},
type: {
type: String as PropType<'solid' | 'dotted' | 'dashed'>,
default: 'solid',
validator: (value: string) => ['solid', 'dotted', 'dashed'].includes(value)
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
}
},
setup (props) {
const { ui, attrs } = useUI('divider', toRef(props, 'ui'), config)
const isHorizontal = computed(() => props.orientation === 'horizontal' )
const wrapperClass = computed(() => {
return twMerge(twJoin(
ui.value.wrapper.base,
isHorizontal.value ? ui.value.wrapper.horizontal : ui.value.wrapper.vertical
), props.class)
})
const containerClass = computed(() => {
return twJoin(
ui.value.container.base,
isHorizontal.value ? ui.value.container.horizontal : ui.value.container.vertical
)
})
const borderClass = computed(() => {
const typeClass = ({
solid: 'border-solid',
dotted: 'border-dotted',
dashed: 'border-dashed'
})[props.type]
return twJoin(
ui.value.border.base,
isHorizontal.value ? ui.value.border.horizontal : ui.value.border.vertical,
isHorizontal.value ? ui.value.border.size.horizontal : ui.value.border.size.vertical,
typeClass
)
})
return {
// eslint-disable-next-line vue/no-dupe-keys
ui,
attrs,
wrapperClass,
containerClass,
borderClass
}
}
})
</script>
Loading

0 comments on commit eb9ce6a

Please sign in to comment.