Link.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Vue 2 Functional Component: https://vuejs.org/v2/guide/render-function.html#Functional-Components */
  2. import { mergeData } from "vue-functional-data-merge"
  3. import getLinkTag, { ANCHOR_TAG, FRAMEWORK_LINK } from "~/assets/js/getLinkTag"
  4. const SmartLink = {
  5. functional: true,
  6. props: {
  7. to: {
  8. type: String,
  9. default: "",
  10. },
  11. exact: {
  12. type: Boolean,
  13. default: false,
  14. },
  15. blank: {
  16. type: Boolean,
  17. default: false,
  18. },
  19. },
  20. // It's a convention to rename `createElement` to `h`
  21. render(h, context) {
  22. const tag = getLinkTag(context.props)
  23. // Map our attributes correctly
  24. const attrs = {}
  25. let on = {}
  26. switch (tag) {
  27. case ANCHOR_TAG:
  28. attrs["aria-label"] = "Link"
  29. attrs.role = "link"
  30. // Map `to` prop to the correct attribute
  31. attrs.href = context.props.to
  32. // Handle `blank` prop
  33. if (context.props.blank) {
  34. attrs.target = "_blank"
  35. attrs.rel = "noopener"
  36. }
  37. // Transform native events to regular events for HTML anchor tag
  38. on = { ...context.data.nativeOn }
  39. delete context.data.nativeOn
  40. break
  41. case FRAMEWORK_LINK:
  42. // Map `to` prop to the correct attribute
  43. attrs.to = context.props.to
  44. // Handle `exact` prop
  45. if (context.props.exact) {
  46. attrs.exact = true
  47. }
  48. break
  49. default:
  50. attrs["aria-label"] = "Button"
  51. attrs.role = "button"
  52. break
  53. }
  54. // Merge our new data with existing ones
  55. const data = mergeData(context.data, { attrs, on })
  56. // Return a new virtual node
  57. return h(tag, data, context.children)
  58. },
  59. }
  60. export default SmartLink