VBadge.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { withDirectives as _withDirectives, mergeProps as _mergeProps, vShow as _vShow, createVNode as _createVNode } from "vue";
  2. // Styles
  3. import "./VBadge.css";
  4. // Components
  5. import { VIcon } from "../VIcon/index.mjs"; // Composables
  6. import { useBackgroundColor, useTextColor } from "../../composables/color.mjs";
  7. import { makeComponentProps } from "../../composables/component.mjs";
  8. import { IconValue } from "../../composables/icons.mjs";
  9. import { useLocale } from "../../composables/locale.mjs";
  10. import { makeLocationProps, useLocation } from "../../composables/location.mjs";
  11. import { makeRoundedProps, useRounded } from "../../composables/rounded.mjs";
  12. import { makeTagProps } from "../../composables/tag.mjs";
  13. import { makeThemeProps, useTheme } from "../../composables/theme.mjs";
  14. import { makeTransitionProps, MaybeTransition } from "../../composables/transition.mjs"; // Utilities
  15. import { toRef } from 'vue';
  16. import { genericComponent, pickWithRest, propsFactory, useRender } from "../../util/index.mjs";
  17. export const makeVBadgeProps = propsFactory({
  18. bordered: Boolean,
  19. color: String,
  20. content: [Number, String],
  21. dot: Boolean,
  22. floating: Boolean,
  23. icon: IconValue,
  24. inline: Boolean,
  25. label: {
  26. type: String,
  27. default: '$vuetify.badge'
  28. },
  29. max: [Number, String],
  30. modelValue: {
  31. type: Boolean,
  32. default: true
  33. },
  34. offsetX: [Number, String],
  35. offsetY: [Number, String],
  36. textColor: String,
  37. ...makeComponentProps(),
  38. ...makeLocationProps({
  39. location: 'top end'
  40. }),
  41. ...makeRoundedProps(),
  42. ...makeTagProps(),
  43. ...makeThemeProps(),
  44. ...makeTransitionProps({
  45. transition: 'scale-rotate-transition'
  46. })
  47. }, 'VBadge');
  48. export const VBadge = genericComponent()({
  49. name: 'VBadge',
  50. inheritAttrs: false,
  51. props: makeVBadgeProps(),
  52. setup(props, ctx) {
  53. const {
  54. backgroundColorClasses,
  55. backgroundColorStyles
  56. } = useBackgroundColor(toRef(props, 'color'));
  57. const {
  58. roundedClasses
  59. } = useRounded(props);
  60. const {
  61. t
  62. } = useLocale();
  63. const {
  64. textColorClasses,
  65. textColorStyles
  66. } = useTextColor(toRef(props, 'textColor'));
  67. const {
  68. themeClasses
  69. } = useTheme();
  70. const {
  71. locationStyles
  72. } = useLocation(props, true, side => {
  73. const base = props.floating ? props.dot ? 2 : 4 : props.dot ? 8 : 12;
  74. return base + (['top', 'bottom'].includes(side) ? +(props.offsetY ?? 0) : ['left', 'right'].includes(side) ? +(props.offsetX ?? 0) : 0);
  75. });
  76. useRender(() => {
  77. const value = Number(props.content);
  78. const content = !props.max || isNaN(value) ? props.content : value <= +props.max ? value : `${props.max}+`;
  79. const [badgeAttrs, attrs] = pickWithRest(ctx.attrs, ['aria-atomic', 'aria-label', 'aria-live', 'role', 'title']);
  80. return _createVNode(props.tag, _mergeProps({
  81. "class": ['v-badge', {
  82. 'v-badge--bordered': props.bordered,
  83. 'v-badge--dot': props.dot,
  84. 'v-badge--floating': props.floating,
  85. 'v-badge--inline': props.inline
  86. }, props.class]
  87. }, attrs, {
  88. "style": props.style
  89. }), {
  90. default: () => [_createVNode("div", {
  91. "class": "v-badge__wrapper"
  92. }, [ctx.slots.default?.(), _createVNode(MaybeTransition, {
  93. "transition": props.transition
  94. }, {
  95. default: () => [_withDirectives(_createVNode("span", _mergeProps({
  96. "class": ['v-badge__badge', themeClasses.value, backgroundColorClasses.value, roundedClasses.value, textColorClasses.value],
  97. "style": [backgroundColorStyles.value, textColorStyles.value, props.inline ? {} : locationStyles.value],
  98. "aria-atomic": "true",
  99. "aria-label": t(props.label, value),
  100. "aria-live": "polite",
  101. "role": "status"
  102. }, badgeAttrs), [props.dot ? undefined : ctx.slots.badge ? ctx.slots.badge?.() : props.icon ? _createVNode(VIcon, {
  103. "icon": props.icon
  104. }, null) : content]), [[_vShow, props.modelValue]])]
  105. })])]
  106. });
  107. });
  108. return {};
  109. }
  110. });
  111. //# sourceMappingURL=VBadge.mjs.map