Skip to content

Commit

Permalink
feat(ButtonGroup): add orientation prop (#603)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
  • Loading branch information
eduayme and benjamincanac committed Sep 7, 2023
1 parent e04c212 commit b3bc6e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/components/content/ComponentCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class="justify-center"
/>
<USelectMenu
v-else-if="prop.type === 'string' && prop.options.length && prop.name !== 'label'"
v-else-if="prop.options.length && prop.name !== 'label'"
v-model="componentProps[prop.name]"
:options="prop.options"
:name="`prop-${prop.name}`"
Expand Down
5 changes: 5 additions & 0 deletions docs/content/2.elements/5.button.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,14 @@ excludedProps:
To stack buttons as a group, use the `ButtonGroup` component.

- To size all the buttons equally, pass the `size` prop
- To change the orientation of the buttons, set the `orientation` prop to `vertical`
- To adjust the rounded or the shadow around buttons, customize with `ui.buttonGroup.rounded` or `ui.buttonGroup.shadow`

::component-card{slug="ButtonGroup"}
---
props:
size: 'sm'
orientation: 'horizontal'
ui:
size:
2xs: ''
Expand All @@ -290,6 +292,9 @@ ui:
md: ''
lg: ''
xl: ''
orientation:
horizontal: ''
vertical: ''
code: |
<UButton label="Action" color="white" />
<UButton icon="i-heroicons-chevron-down-20-solid" color="gray" />
Expand Down
42 changes: 29 additions & 13 deletions src/runtime/components/elements/ButtonGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export default defineComponent({
return Object.keys(appConfig.ui.button.size).includes(value)
}
},
orientation: {
type: String as PropType<'horizontal' | 'vertical'>,
default: 'horizontal',
validator (value: string) {
return ['horizontal', 'vertical'].includes(value)
}
},
ui: {
type: Object as PropType<Partial<typeof appConfig.ui.buttonGroup>>,
default: () => ({})
Expand All @@ -33,21 +40,30 @@ export default defineComponent({

const children = computed(() => getSlotsChildren(slots))

const rounded = computed(() => ({
'rounded-none': { left: 'rounded-s-none', right: 'rounded-e-none' },
'rounded-sm': { left: 'rounded-s-sm', right: 'rounded-e-sm' },
rounded: { left: 'rounded-s', right: 'rounded-e' },
'rounded-md': { left: 'rounded-s-md', right: 'rounded-e-md' },
'rounded-lg': { left: 'rounded-s-lg', right: 'rounded-e-lg' },
'rounded-xl': { left: 'rounded-s-xl', right: 'rounded-e-xl' },
'rounded-2xl': { left: 'rounded-s-2xl', right: 'rounded-e-2xl' },
'rounded-3xl': { left: 'rounded-s-3xl', right: 'rounded-e-3xl' },
'rounded-full': { left: 'rounded-s-full', right: 'rounded-e-full' }
}[ui.value.rounded]))
const rounded = computed(() => {
const roundedMap = {
'rounded-none': { horizontal: { left: 'rounded-s-none', right: 'rounded-e-none' }, vertical: { top: 'rounded-t-none', bottom: 'rounded-b-none' } },
'rounded-sm': { horizontal: { left: 'rounded-s-sm', right: 'rounded-e-sm' }, vertical: { top: 'rounded-t-sm', bottom: 'rounded-b-sm' } },
rounded: { horizontal: { left: 'rounded-s', right: 'rounded-e' }, vertical: { top: 'rounded-t', bottom: 'rounded-b' } },
'rounded-md': { horizontal: { left: 'rounded-s-md', right: 'rounded-e-md' }, vertical: { top: 'rounded-t-md', bottom: 'rounded-b-md' } },
'rounded-lg': { horizontal: { left: 'rounded-s-lg', right: 'rounded-e-lg' }, vertical: { top: 'rounded-t-lg', bottom: 'rounded-b-lg' } },
'rounded-xl': { horizontal: { left: 'rounded-s-xl', right: 'rounded-e-xl' }, vertical: { top: 'rounded-t-xl', bottom: 'rounded-b-xl' } },
'rounded-2xl': { horizontal: { left: 'rounded-s-2xl', right: 'rounded-e-2xl' }, vertical: { top: 'rounded-t-2xl', bottom: 'rounded-b-2xl' } },
'rounded-3xl': { horizontal: { left: 'rounded-s-3xl', right: 'rounded-e-3xl' }, vertical: { top: 'rounded-t-3xl', bottom: 'rounded-b-3xl' } },
'rounded-full': { horizontal: { left: 'rounded-s-full', right: 'rounded-e-full' }, vertical: { top: 'rounded-t-full', bottom: 'rounded-b-full' } }
}
return roundedMap[ui.value.rounded][props.orientation]
})

const clones = computed(() => children.value.map((node, index) => {
const vProps: any = {}

if (props.orientation === 'vertical') {
ui.value.wrapper = 'flex flex-col -space-y-px'
} else {
ui.value.wrapper = 'inline-flex -space-x-px'
}

if (props.size) {
vProps.size = props.size
}
Expand All @@ -57,11 +73,11 @@ export default defineComponent({
vProps.ui.base = '!shadow-none'

if (index === 0) {
vProps.ui.rounded += ` ${rounded.value.left}`
vProps.ui.rounded += ` ${rounded.value.left || rounded.value.top}`
}

if (index === children.value.length - 1) {
vProps.ui.rounded += ` ${rounded.value.right}`
vProps.ui.rounded += ` ${rounded.value.right || rounded.value.bottom}`
}

return cloneVNode(node, vProps)
Expand Down

0 comments on commit b3bc6e2

Please sign in to comment.