VBtn.mjs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { withDirectives as _withDirectives, resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  2. // Styles
  3. import "./VBtn.css";
  4. // Components
  5. import { VBtnToggleSymbol } from "../VBtnToggle/VBtnToggle.mjs";
  6. import { VDefaultsProvider } from "../VDefaultsProvider/index.mjs";
  7. import { VIcon } from "../VIcon/index.mjs";
  8. import { VProgressCircular } from "../VProgressCircular/index.mjs"; // Composables
  9. import { makeBorderProps, useBorder } from "../../composables/border.mjs";
  10. import { makeComponentProps } from "../../composables/component.mjs";
  11. import { makeDensityProps, useDensity } from "../../composables/density.mjs";
  12. import { makeDimensionProps, useDimension } from "../../composables/dimensions.mjs";
  13. import { makeElevationProps, useElevation } from "../../composables/elevation.mjs";
  14. import { makeGroupItemProps, useGroupItem } from "../../composables/group.mjs";
  15. import { IconValue } from "../../composables/icons.mjs";
  16. import { makeLoaderProps, useLoader } from "../../composables/loader.mjs";
  17. import { makeLocationProps, useLocation } from "../../composables/location.mjs";
  18. import { makePositionProps, usePosition } from "../../composables/position.mjs";
  19. import { makeRoundedProps, useRounded } from "../../composables/rounded.mjs";
  20. import { makeRouterProps, useLink } from "../../composables/router.mjs";
  21. import { useSelectLink } from "../../composables/selectLink.mjs";
  22. import { makeSizeProps, useSize } from "../../composables/size.mjs";
  23. import { makeTagProps } from "../../composables/tag.mjs";
  24. import { makeThemeProps, provideTheme } from "../../composables/theme.mjs";
  25. import { genOverlays, makeVariantProps, useVariant } from "../../composables/variant.mjs"; // Directives
  26. import { Ripple } from "../../directives/ripple/index.mjs"; // Utilities
  27. import { computed } from 'vue';
  28. import { genericComponent, propsFactory, useRender } from "../../util/index.mjs"; // Types
  29. export const makeVBtnProps = propsFactory({
  30. active: {
  31. type: Boolean,
  32. default: undefined
  33. },
  34. symbol: {
  35. type: null,
  36. default: VBtnToggleSymbol
  37. },
  38. flat: Boolean,
  39. icon: [Boolean, String, Function, Object],
  40. prependIcon: IconValue,
  41. appendIcon: IconValue,
  42. block: Boolean,
  43. stacked: Boolean,
  44. ripple: {
  45. type: [Boolean, Object],
  46. default: true
  47. },
  48. text: String,
  49. ...makeBorderProps(),
  50. ...makeComponentProps(),
  51. ...makeDensityProps(),
  52. ...makeDimensionProps(),
  53. ...makeElevationProps(),
  54. ...makeGroupItemProps(),
  55. ...makeLoaderProps(),
  56. ...makeLocationProps(),
  57. ...makePositionProps(),
  58. ...makeRoundedProps(),
  59. ...makeRouterProps(),
  60. ...makeSizeProps(),
  61. ...makeTagProps({
  62. tag: 'button'
  63. }),
  64. ...makeThemeProps(),
  65. ...makeVariantProps({
  66. variant: 'elevated'
  67. })
  68. }, 'VBtn');
  69. export const VBtn = genericComponent()({
  70. name: 'VBtn',
  71. directives: {
  72. Ripple
  73. },
  74. props: makeVBtnProps(),
  75. emits: {
  76. 'group:selected': val => true
  77. },
  78. setup(props, _ref) {
  79. let {
  80. attrs,
  81. slots
  82. } = _ref;
  83. const {
  84. themeClasses
  85. } = provideTheme(props);
  86. const {
  87. borderClasses
  88. } = useBorder(props);
  89. const {
  90. colorClasses,
  91. colorStyles,
  92. variantClasses
  93. } = useVariant(props);
  94. const {
  95. densityClasses
  96. } = useDensity(props);
  97. const {
  98. dimensionStyles
  99. } = useDimension(props);
  100. const {
  101. elevationClasses
  102. } = useElevation(props);
  103. const {
  104. loaderClasses
  105. } = useLoader(props);
  106. const {
  107. locationStyles
  108. } = useLocation(props);
  109. const {
  110. positionClasses
  111. } = usePosition(props);
  112. const {
  113. roundedClasses
  114. } = useRounded(props);
  115. const {
  116. sizeClasses,
  117. sizeStyles
  118. } = useSize(props);
  119. const group = useGroupItem(props, props.symbol, false);
  120. const link = useLink(props, attrs);
  121. const isActive = computed(() => {
  122. if (props.active !== undefined) {
  123. return props.active;
  124. }
  125. if (link.isLink.value) {
  126. return link.isActive?.value;
  127. }
  128. return group?.isSelected.value;
  129. });
  130. const isDisabled = computed(() => group?.disabled.value || props.disabled);
  131. const isElevated = computed(() => {
  132. return props.variant === 'elevated' && !(props.disabled || props.flat || props.border);
  133. });
  134. const valueAttr = computed(() => {
  135. if (props.value === undefined) return undefined;
  136. return Object(props.value) === props.value ? JSON.stringify(props.value, null, 0) : props.value;
  137. });
  138. function onClick(e) {
  139. if (isDisabled.value || link.isLink.value && (e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0 || attrs.target === '_blank')) return;
  140. link.navigate?.(e);
  141. group?.toggle();
  142. }
  143. useSelectLink(link, group?.select);
  144. useRender(() => {
  145. const Tag = link.isLink.value ? 'a' : props.tag;
  146. const hasPrepend = !!(props.prependIcon || slots.prepend);
  147. const hasAppend = !!(props.appendIcon || slots.append);
  148. const hasIcon = !!(props.icon && props.icon !== true);
  149. const hasColor = group?.isSelected.value && (!link.isLink.value || link.isActive?.value) || !group || link.isActive?.value;
  150. return _withDirectives(_createVNode(Tag, {
  151. "type": Tag === 'a' ? undefined : 'button',
  152. "class": ['v-btn', group?.selectedClass.value, {
  153. 'v-btn--active': isActive.value,
  154. 'v-btn--block': props.block,
  155. 'v-btn--disabled': isDisabled.value,
  156. 'v-btn--elevated': isElevated.value,
  157. 'v-btn--flat': props.flat,
  158. 'v-btn--icon': !!props.icon,
  159. 'v-btn--loading': props.loading,
  160. 'v-btn--stacked': props.stacked
  161. }, themeClasses.value, borderClasses.value, hasColor ? colorClasses.value : undefined, densityClasses.value, elevationClasses.value, loaderClasses.value, positionClasses.value, roundedClasses.value, sizeClasses.value, variantClasses.value, props.class],
  162. "style": [hasColor ? colorStyles.value : undefined, dimensionStyles.value, locationStyles.value, sizeStyles.value, props.style],
  163. "disabled": isDisabled.value || undefined,
  164. "href": link.href.value,
  165. "onClick": onClick,
  166. "value": valueAttr.value
  167. }, {
  168. default: () => [genOverlays(true, 'v-btn'), !props.icon && hasPrepend && _createVNode("span", {
  169. "key": "prepend",
  170. "class": "v-btn__prepend"
  171. }, [!slots.prepend ? _createVNode(VIcon, {
  172. "key": "prepend-icon",
  173. "icon": props.prependIcon
  174. }, null) : _createVNode(VDefaultsProvider, {
  175. "key": "prepend-defaults",
  176. "disabled": !props.prependIcon,
  177. "defaults": {
  178. VIcon: {
  179. icon: props.prependIcon
  180. }
  181. }
  182. }, slots.prepend)]), _createVNode("span", {
  183. "class": "v-btn__content",
  184. "data-no-activator": ""
  185. }, [!slots.default && hasIcon ? _createVNode(VIcon, {
  186. "key": "content-icon",
  187. "icon": props.icon
  188. }, null) : _createVNode(VDefaultsProvider, {
  189. "key": "content-defaults",
  190. "disabled": !hasIcon,
  191. "defaults": {
  192. VIcon: {
  193. icon: props.icon
  194. }
  195. }
  196. }, {
  197. default: () => [slots.default?.() ?? props.text]
  198. })]), !props.icon && hasAppend && _createVNode("span", {
  199. "key": "append",
  200. "class": "v-btn__append"
  201. }, [!slots.append ? _createVNode(VIcon, {
  202. "key": "append-icon",
  203. "icon": props.appendIcon
  204. }, null) : _createVNode(VDefaultsProvider, {
  205. "key": "append-defaults",
  206. "disabled": !props.appendIcon,
  207. "defaults": {
  208. VIcon: {
  209. icon: props.appendIcon
  210. }
  211. }
  212. }, slots.append)]), !!props.loading && _createVNode("span", {
  213. "key": "loader",
  214. "class": "v-btn__loader"
  215. }, [slots.loader?.() ?? _createVNode(VProgressCircular, {
  216. "color": typeof props.loading === 'boolean' ? undefined : props.loading,
  217. "indeterminate": true,
  218. "size": "23",
  219. "width": "2"
  220. }, null)])]
  221. }), [[_resolveDirective("ripple"), !isDisabled.value && props.ripple, null]]);
  222. });
  223. return {};
  224. }
  225. });
  226. //# sourceMappingURL=VBtn.mjs.map