| | |
| | | return <button id="btn">Click me</button> |
| | | } |
| | | |
| | | YourComponent.beforeDOM = ` |
| | | YourComponent.beforeDOMLoaded = ` |
| | | console.log("hello from before the page loads!") |
| | | ` |
| | | |
| | | YourComponent.afterDOM = ` |
| | | YourComponent.afterDOMLoaded = ` |
| | | document.getElementById('btn').onclick = () => { |
| | | alert('button clicked!') |
| | | } |
| | |
| | | // do page specific logic here |
| | | // e.g. attach event listeners |
| | | const toggleSwitch = document.querySelector("#switch") as HTMLInputElement |
| | | toggleSwitch.removeEventListener("change", switchTheme) |
| | | toggleSwitch.addEventListener("change", switchTheme) |
| | | window.addCleanup(() => toggleSwitch.removeEventListener("change", switchTheme)) |
| | | }) |
| | | ``` |
| | | |
| | | It is best practice to also unmount any existing event handlers to prevent memory leaks. |
| | | You can also add the equivalent of a `beforeunload` event for [[SPA Routing]] via the `prenav` event. |
| | | |
| | | ```ts |
| | | document.addEventListener("prenav", () => { |
| | | // executed after an SPA navigation is triggered but |
| | | // before the page is replaced |
| | | // one usage pattern is to store things in sessionStorage |
| | | // in the prenav and then conditionally load then in the consequent |
| | | // nav |
| | | }) |
| | | ``` |
| | | |
| | | It is best practice to track any event handlers via `window.addCleanup` to prevent memory leaks. |
| | | This will get called on page navigation. |
| | | |
| | | #### Importing Code |
| | | |
| | |
| | | return <button id="btn">Click me</button> |
| | | } |
| | | |
| | | YourComponent.afterDOM = script |
| | | YourComponent.afterDOMLoaded = script |
| | | return YourComponent |
| | | }) satisfies QuartzComponentConstructor |
| | | ``` |
| | |
| | | As Quartz components are just functions that return React components, you can compositionally use them in other Quartz components. |
| | | |
| | | ```tsx title="quartz/components/AnotherComponent.tsx" |
| | | import YourComponent from "./YourComponent" |
| | | import YourComponentConstructor from "./YourComponent" |
| | | |
| | | export default (() => { |
| | | const YourComponent = YourComponentConstructor() |
| | | |
| | | function AnotherComponent(props: QuartzComponentProps) { |
| | | return ( |
| | | <div> |