Link.js 1.6 KB

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