@fiscozen/dropdown 0.1.13 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/FzDropdown.vue +21 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fiscozen/dropdown",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Design System Dropdown component",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <FzFloating :isOpen :position="floatingPosition">
2
+ <FzFloating :isOpen :position="floatingPosition" ref="container">
3
3
  <template #opener>
4
4
  <FzButton
5
5
  icon-position="after"
@@ -15,7 +15,7 @@
15
15
  </template>
16
16
 
17
17
  <script setup lang="ts">
18
- import { computed, ref } from 'vue'
18
+ import { ComponentPublicInstance, computed, onMounted, onUnmounted, ref } from 'vue'
19
19
  import { FzButton, ButtonSize } from '@fiscozen/button'
20
20
  import { FzActionlist, FzActionlistProps, ActionlistItem } from '@fiscozen/actionlist'
21
21
  import { FzFloating } from '@fiscozen/composables'
@@ -54,9 +54,28 @@ const emit = defineEmits<{
54
54
  }>()
55
55
 
56
56
  const isOpen = ref(false)
57
+ const container = ref<ComponentPublicInstance>()
57
58
  const buttonIconName = computed(() => (isOpen.value ? 'angle-up' : 'angle-down'))
58
59
  const floatingPosition = computed(() => (props.align === 'left' ? 'bottom-start' : 'bottom-end'))
59
60
 
61
+ onMounted(() => {
62
+ document.addEventListener('click', handleClickOutside)
63
+ })
64
+
65
+ onUnmounted(() => {
66
+ document.removeEventListener('click', handleClickOutside)
67
+ })
68
+
69
+ function handleClickOutside(event: MouseEvent) {
70
+ if (!container.value) return
71
+
72
+ const isClickOutside =
73
+ container.value.$el !== event.target && !container.value.$el.contains(event.target)
74
+ if (isClickOutside) {
75
+ isOpen.value = false
76
+ }
77
+ }
78
+
60
79
  function handleActionClick(index: number, action: ActionlistItem) {
61
80
  emit('fzaction:click', index, action)
62
81
  if (props.closeOnActionClick) {