-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathNcChip.vue
268 lines (236 loc) · 6.24 KB
/
NcChip.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!--
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<docs>
### Basic usage
```vue
<template>
<div style="display: flex; gap: 8px; flex-wrap: wrap;">
<NcChip text="Notes.txt" />
<NcChip text="Files" :icon-path="mdiFile" />
<NcChip text="Color" :icon-path="mdiPalette" variant="tertiary" />
<NcChip text="Current time" :icon-path="mdiClock" no-close variant="primary" />
</div>
</template>
<script>
import { mdiClock, mdiFile, mdiPalette } from '@mdi/js'
export default {
setup() {
return {
mdiClock,
mdiFile,
mdiPalette,
}
}
}
</script>
```
### Advanced usage
It is also possible to use custom components for the icon by using the `icon` slot.
In this example we are using the `NcAvatar` component to render the users avatar as the icon.
*Hint: If you use round icons like avatars you should set their size to `24px` (or use CSS variable `--chip-size`) to make them fully fill and align with the the chip*
Also it is possible to pass custom actions.
```vue
<template>
<NcChip>
<!-- The icon slot allow to use custom components as the chip icon -->
<template #icon>
<NcAvatar :size="24" user="jdoe" display-name="J. Doe" />
</template>
<!-- The actions slot allows to add custom actions -->
<template #actions>
<NcActionButton>
<template #icon>
<ContactsIcon :size="20" />
</template>
Add to contacts
</NcActionButton>
</template>
<!-- The default slot can be used for add content, just like the `text` prop -->
J. Doe
</NcChip>
</template>
<script>
import ContactsIcon from 'vue-material-design-icons/Contacts.vue'
export default {
components: {
ContactsIcon,
},
}
</script>
```
</docs>
<template>
<div class="nc-chip"
:class="{
[`nc-chip--${variant}`]: true,
'nc-chip--no-actions': noClose && !hasActions(),
'nc-chip--no-icon': !hasIcon(),
}">
<span v-if="hasIcon()" class="nc-chip__icon">
<slot name="icon">
<!-- The default icon wrapper uses a size of 18px to ensure the icon is not clipped by the round chip style -->
<NcIconSvgWrapper v-if="iconPath || iconSvg"
inline
:path="iconPath"
:svg="iconPath ? undefined : iconSvg"
:size="18" />
</slot>
</span>
<span class="nc-chip__text">
<slot>{{ text }}</slot>
</span>
<NcActions v-if="canClose || hasActions()"
class="nc-chip__actions"
:force-menu="!canClose"
variant="tertiary-no-background">
<NcActionButton v-if="canClose"
close-after-click
@click="onClose">
<template #icon>
<NcIconSvgWrapper :path="mdiClose" :size="20" />
</template>
{{ ariaLabelClose }}
</NcActionButton>
<slot name="actions" />
</NcActions>
</div>
</template>
<script setup lang="ts">
import type { Slot } from 'vue'
import { mdiClose } from '@mdi/js'
import { computed } from 'vue'
import { t } from '../../l10n.js'
import NcActions from '../NcActions/NcActions.vue'
import NcActionButton from '../NcActionButton/NcActionButton.vue'
import NcIconSvgWrapper from '../NcIconSvgWrapper/NcIconSvgWrapper.vue'
const props = withDefaults(defineProps<{
/**
* aria label to set on the close button
* @default 'Close'
*/
ariaLabelClose?: string
/**
* Main text of the chip.
*/
text?: string
/**
* SVG path of the icon to use, this takes precedence over `iconSVG`.
* For example icon paths from `@mdi/js` can be used.
*/
iconPath?: string
/**
* Inline SVG to use as the icon
*/
iconSvg?: string
/**
* Set to true to prevent the close button to be shown
*/
noClose?: boolean
/**
* Set the chips design variant-
*
* This sets the background style of the chip, similar to NcButton's `variant`.
* @default 'secondary'
* @since 8.23.0
*/
variant: 'primary' | 'secondary' | 'tertiary'
}>(), {
ariaLabelClose: t('Close'),
iconPath: undefined,
iconSvg: undefined,
text: '',
variant: 'secondary',
})
const emit = defineEmits<{
/**
* Emitted when the close button is clicked
*/
close: []
}>()
const slots = defineSlots<{
/**
* The actions slot can be used to add custom actions to the chips actions.
*/
actions?: Slot
/**
* The default slot can be used to set the text that is shown.
*/
default?: Slot
/**
* The icon slot can be used to set the chip icon.
*
* Make sure that the icon is not exceeding a height of `24px`.
* For round icons a exact size of `24px` is recommended.
*/
icon?: Slot
}>()
const canClose = computed(() => !props.noClose)
const hasActions = () => Boolean(slots.actions?.())
const hasIcon = () => Boolean(props.iconPath || props.iconSvg || !!slots.icon?.())
/**
* Handle closing the chip (pressing the X-button)
*/
function onClose() {
emit('close')
}
</script>
<style scoped lang="scss">
.nc-chip {
--chip-size: 24px;
--chip-radius: calc(var(--chip-size) / 2);
// Setup size of wrapper
height: var(--chip-size);
max-width: fit-content;
display: flex;
flex-direction: row;
align-items: center;
border-radius: var(--chip-radius);
background-color: var(--color-background-hover);
&--primary {
background-color: var(--color-primary-element);
color: var(--color-primary-element-text);
}
&--secondary {
background-color: var(--color-primary-element-light);
color: var(--color-primary-element-light-text);
}
&--no-actions &__text {
// If there are no actions we need to add some padding to ensure the text is not cut-off
padding-inline-end: calc(1.5 * var(--default-grid-baseline));
}
&--no-icon &__text {
// Add some more space to the border
padding-inline-start: calc(1.5 * var(--default-grid-baseline));
}
&__text {
// Allow to grow the text
// this is only used if an app forces a width of the chip
flex: 1 auto;
overflow: hidden;
text-overflow: ellipsis;
text-wrap: nowrap;
}
&__icon {
// Do neither grow nor shrink, size is fixed
flex: 0 0 var(--chip-size);
margin-inline-end: var(--default-grid-baseline);
line-height: 1;
display: flex;
align-items: center;
justify-content: center;
// Force size
overflow: hidden;
height: var(--chip-size);
width: var(--chip-size);
}
&__actions {
// Do neither grow nor shrink, size is fixed
flex: 0 0 var(--chip-size);
// Adjust action size to match chip size
--default-clickable-area: var(--chip-size);
--border-radius-element: var(--chip-radius);
}
}
</style>