icons.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
  2. // Icons
  3. import { aliases, mdi } from "../iconsets/mdi.mjs"; // Utilities
  4. import { computed, inject, unref } from 'vue';
  5. import { defineComponent, genericComponent, mergeDeep, propsFactory } from "../util/index.mjs"; // Types
  6. export const IconValue = [String, Function, Object, Array];
  7. export const IconSymbol = Symbol.for('vuetify:icons');
  8. export const makeIconProps = propsFactory({
  9. icon: {
  10. type: IconValue
  11. },
  12. // Could not remove this and use makeTagProps, types complained because it is not required
  13. tag: {
  14. type: String,
  15. required: true
  16. }
  17. }, 'icon');
  18. export const VComponentIcon = genericComponent()({
  19. name: 'VComponentIcon',
  20. props: makeIconProps(),
  21. setup(props, _ref) {
  22. let {
  23. slots
  24. } = _ref;
  25. return () => {
  26. const Icon = props.icon;
  27. return _createVNode(props.tag, null, {
  28. default: () => [props.icon ? _createVNode(Icon, null, null) : slots.default?.()]
  29. });
  30. };
  31. }
  32. });
  33. export const VSvgIcon = defineComponent({
  34. name: 'VSvgIcon',
  35. inheritAttrs: false,
  36. props: makeIconProps(),
  37. setup(props, _ref2) {
  38. let {
  39. attrs
  40. } = _ref2;
  41. return () => {
  42. return _createVNode(props.tag, _mergeProps(attrs, {
  43. "style": null
  44. }), {
  45. default: () => [_createVNode("svg", {
  46. "class": "v-icon__svg",
  47. "xmlns": "http://www.w3.org/2000/svg",
  48. "viewBox": "0 0 24 24",
  49. "role": "img",
  50. "aria-hidden": "true"
  51. }, [Array.isArray(props.icon) ? props.icon.map(path => Array.isArray(path) ? _createVNode("path", {
  52. "d": path[0],
  53. "fill-opacity": path[1]
  54. }, null) : _createVNode("path", {
  55. "d": path
  56. }, null)) : _createVNode("path", {
  57. "d": props.icon
  58. }, null)])]
  59. });
  60. };
  61. }
  62. });
  63. export const VLigatureIcon = defineComponent({
  64. name: 'VLigatureIcon',
  65. props: makeIconProps(),
  66. setup(props) {
  67. return () => {
  68. return _createVNode(props.tag, null, {
  69. default: () => [props.icon]
  70. });
  71. };
  72. }
  73. });
  74. export const VClassIcon = defineComponent({
  75. name: 'VClassIcon',
  76. props: makeIconProps(),
  77. setup(props) {
  78. return () => {
  79. return _createVNode(props.tag, {
  80. "class": props.icon
  81. }, null);
  82. };
  83. }
  84. });
  85. export const defaultSets = {
  86. svg: {
  87. component: VSvgIcon
  88. },
  89. class: {
  90. component: VClassIcon
  91. }
  92. };
  93. // Composables
  94. export function createIcons(options) {
  95. return mergeDeep({
  96. defaultSet: 'mdi',
  97. sets: {
  98. ...defaultSets,
  99. mdi
  100. },
  101. aliases: {
  102. ...aliases,
  103. /* eslint-disable max-len */
  104. vuetify: ['M8.2241 14.2009L12 21L22 3H14.4459L8.2241 14.2009Z', ['M7.26303 12.4733L7.00113 12L2 3H12.5261C12.5261 3 12.5261 3 12.5261 3L7.26303 12.4733Z', 0.6]],
  105. 'vuetify-outline': 'svg:M7.26 12.47 12.53 3H2L7.26 12.47ZM14.45 3 8.22 14.2 12 21 22 3H14.45ZM18.6 5 12 16.88 10.51 14.2 15.62 5ZM7.26 8.35 5.4 5H9.13L7.26 8.35Z'
  106. /* eslint-enable max-len */
  107. }
  108. }, options);
  109. }
  110. export const useIcon = props => {
  111. const icons = inject(IconSymbol);
  112. if (!icons) throw new Error('Missing Vuetify Icons provide!');
  113. const iconData = computed(() => {
  114. const iconAlias = unref(props);
  115. if (!iconAlias) return {
  116. component: VComponentIcon
  117. };
  118. let icon = iconAlias;
  119. if (typeof icon === 'string') {
  120. icon = icon.trim();
  121. if (icon.startsWith('$')) {
  122. icon = icons.aliases?.[icon.slice(1)];
  123. }
  124. }
  125. if (!icon) throw new Error(`Could not find aliased icon "${iconAlias}"`);
  126. if (Array.isArray(icon)) {
  127. return {
  128. component: VSvgIcon,
  129. icon
  130. };
  131. } else if (typeof icon !== 'string') {
  132. return {
  133. component: VComponentIcon,
  134. icon
  135. };
  136. }
  137. const iconSetName = Object.keys(icons.sets).find(setName => typeof icon === 'string' && icon.startsWith(`${setName}:`));
  138. const iconName = iconSetName ? icon.slice(iconSetName.length + 1) : icon;
  139. const iconSet = icons.sets[iconSetName ?? icons.defaultSet];
  140. return {
  141. component: iconSet.component,
  142. icon: iconName
  143. };
  144. });
  145. return {
  146. iconData
  147. };
  148. };
  149. //# sourceMappingURL=icons.mjs.map