CollapseButton.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed } from 'vue'
  4. import { useTouchDevice } from '#shared/composables/useTouchDevice.ts'
  5. import { EnumTextDirection } from '#shared/graphql/types.ts'
  6. import { useLocaleStore } from '#shared/stores/locale.ts'
  7. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  8. const { isTouchDevice } = useTouchDevice()
  9. interface Props {
  10. ownerId: string
  11. isCollapsed?: boolean
  12. group?: 'heading' | 'sidebar'
  13. orientation?: 'horizontal' | 'vertical'
  14. }
  15. const props = withDefaults(defineProps<Props>(), {
  16. orientation: 'horizontal',
  17. isCollapsed: false,
  18. })
  19. defineEmits<{
  20. 'toggle-collapse': [MouseEvent]
  21. }>()
  22. const locale = useLocaleStore()
  23. const collapseButtonIcon = computed(() => {
  24. if (props.orientation === 'vertical')
  25. return props.isCollapsed ? 'arrows-expand' : 'arrows-collapse'
  26. if (locale.localeData?.dir === EnumTextDirection.Rtl)
  27. return props.isCollapsed ? 'arrow-bar-left' : 'arrow-bar-right'
  28. return props.isCollapsed ? 'arrow-bar-right' : 'arrow-bar-left'
  29. })
  30. const parentGroupClass = computed(() => {
  31. // Tailwindcss must be able to scan the class names to generate CSS
  32. // https://tailwindcss.com/docs/content-configuration#dynamic-class-names
  33. switch (props.group) {
  34. case 'heading':
  35. return 'group-hover/heading:opacity-100'
  36. case 'sidebar':
  37. return 'group-hover/sidebar:opacity-100'
  38. default:
  39. return ''
  40. }
  41. })
  42. </script>
  43. <template>
  44. <div>
  45. <CommonButton
  46. :class="[
  47. { 'opacity-0 transition-opacity': !isTouchDevice && parentGroupClass },
  48. 'focus:opacity-100',
  49. parentGroupClass,
  50. ]"
  51. class="collapse-button"
  52. :icon="collapseButtonIcon"
  53. :aria-expanded="!props.isCollapsed"
  54. :aria-controls="ownerId"
  55. :aria-label="
  56. props.isCollapsed
  57. ? $t('Expand this element')
  58. : $t('Collapse this element')
  59. "
  60. size="small"
  61. variant="subtle"
  62. @click="$emit('toggle-collapse', $event)"
  63. />
  64. </div>
  65. </template>