Link.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // Map `to` prop to the correct attribute
  30. attrs.href = context.props.to
  31. // Handle `blank` prop
  32. if (context.props.blank) {
  33. attrs.target = "_blank"
  34. attrs.rel = "noopener"
  35. }
  36. // Transform native events to regular events for HTML anchor tag
  37. on = { ...context.data.nativeOn }
  38. delete context.data.nativeOn
  39. break
  40. case FRAMEWORK_LINK:
  41. // Map `to` prop to the correct attribute
  42. attrs.to = context.props.to
  43. // Handle `exact` prop
  44. if (context.props.exact) {
  45. attrs.exact = true
  46. }
  47. break
  48. default:
  49. attrs["aria-label"] = "Button"
  50. break
  51. }
  52. // Merge our new data with existing ones
  53. const data = mergeData(context.data, { attrs, on })
  54. // Return a new virtual node
  55. return h(tag, data, context.children)
  56. },
  57. }
  58. export default SmartLink