reusable-snippets.mdx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ---
  2. title: "Reusable snippets"
  3. description: "Reusable, custom snippets to keep content in sync"
  4. icon: "recycle"
  5. ---
  6. import SnippetIntro from "/snippets/snippet-intro.mdx"
  7. <SnippetIntro />
  8. ## Creating a custom snippet
  9. **Pre-condition**: You must create your snippet file in the `snippets` directory.
  10. <Note>
  11. Any page in the `snippets` directory will be treated as a snippet and will not be rendered into a standalone page. If
  12. you want to create a standalone page from the snippet, import the snippet into another file and call it as a
  13. component.
  14. </Note>
  15. ### Default export
  16. 1. Add content to your snippet file that you want to re-use across multiple
  17. locations. Optionally, you can add variables that can be filled in via props
  18. when you import the snippet.
  19. ```mdx snippets/my-snippet.mdx
  20. Hello world! This is my content I want to reuse across pages. My keyword of the
  21. day is {word}.
  22. ```
  23. <Warning>
  24. The content that you want to reuse must be inside the `snippets` directory in order for the import to work.
  25. </Warning>
  26. 2. Import the snippet into your destination file.
  27. ```mdx destination-file.mdx
  28. ---
  29. title: My title
  30. description: My Description
  31. ---
  32. import MySnippet from "/snippets/path/to/my-snippet.mdx"
  33. ## Header
  34. Lorem impsum dolor sit amet.
  35. <MySnippet word="bananas" />
  36. ```
  37. ### Reusable variables
  38. 1. Export a variable from your snippet file:
  39. ```mdx snippets/path/to/custom-variables.mdx
  40. export const myName = "my name"
  41. export const myObject = { fruit: "strawberries" }
  42. ;
  43. ```
  44. 2. Import the snippet from your destination file and use the variable:
  45. ```mdx destination-file.mdx
  46. ---
  47. title: My title
  48. description: My Description
  49. ---
  50. import { myName, myObject } from "/snippets/path/to/custom-variables.mdx"
  51. Hello, my name is {myName} and I like {myObject.fruit}.
  52. ```
  53. ### Reusable components
  54. 1. Inside your snippet file, create a component that takes in props by exporting
  55. your component in the form of an arrow function.
  56. ```mdx snippets/custom-component.mdx
  57. export const MyComponent = ({ title }) => (
  58. <div>
  59. <h1>{title}</h1>
  60. <p>... snippet content ...</p>
  61. </div>
  62. )
  63. ;
  64. ```
  65. <Warning>
  66. MDX does not compile inside the body of an arrow function. Stick to HTML syntax when you can or use a default export
  67. if you need to use MDX.
  68. </Warning>
  69. 2. Import the snippet into your destination file and pass in the props
  70. ```mdx destination-file.mdx
  71. ---
  72. title: My title
  73. description: My Description
  74. ---
  75. import { MyComponent } from "/snippets/custom-component.mdx"
  76. Lorem ipsum dolor sit amet.
  77. <MyComponent title={"Custom title"} />
  78. ```