From 688021538afb132bb4bf5c114d239f9d73b736fb Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Tue, 4 Jul 2023 09:18:17 +0000 Subject: [PATCH 1/2] Add static toggle --- packages/mdx/dev/content/static-toggle.mdx | 11 + packages/mdx/src/components.tsx | 7 + packages/mdx/src/highlighter/index.tsx | 1 + packages/mdx/src/mdx-client/scrollycoding.tsx | 13 +- packages/mdx/src/mdx-client/ssmq.tsx | 199 ++++++++++-------- 5 files changed, 134 insertions(+), 97 deletions(-) create mode 100644 packages/mdx/dev/content/static-toggle.mdx diff --git a/packages/mdx/dev/content/static-toggle.mdx b/packages/mdx/dev/content/static-toggle.mdx new file mode 100644 index 00000000..ece11f00 --- /dev/null +++ b/packages/mdx/dev/content/static-toggle.mdx @@ -0,0 +1,11 @@ + + + + +Hello + +```js +1 +``` + + diff --git a/packages/mdx/src/components.tsx b/packages/mdx/src/components.tsx index 2032ebc4..2c7f63f4 100644 --- a/packages/mdx/src/components.tsx +++ b/packages/mdx/src/components.tsx @@ -15,6 +15,10 @@ import { import { Preview } from "./mdx-client/preview" import { InlineCode } from "./mdx-client/inline-code" import type { MDXComponents } from "mdx/types" +import { + useStaticToggle, + StaticToggle, +} from "./mdx-client/ssmq" export { Code, @@ -30,6 +34,8 @@ export { InlineCode, CodeSlot, PreviewSlot, + useStaticToggle, + StaticToggle, } export const CH: MDXComponents = { @@ -46,6 +52,7 @@ export const CH: MDXComponents = { InlineCode, CodeSlot, PreviewSlot, + StaticToggle, } import { MiniBrowser } from "./mini-browser" diff --git a/packages/mdx/src/highlighter/index.tsx b/packages/mdx/src/highlighter/index.tsx index 6cad3d76..90123a75 100644 --- a/packages/mdx/src/highlighter/index.tsx +++ b/packages/mdx/src/highlighter/index.tsx @@ -44,6 +44,7 @@ export async function highlight({ ) warnings.add(lang) } + // potential endless loop return highlight({ code, lang: "text", theme }) } } diff --git a/packages/mdx/src/mdx-client/scrollycoding.tsx b/packages/mdx/src/mdx-client/scrollycoding.tsx index a385b908..bce5c0b6 100644 --- a/packages/mdx/src/mdx-client/scrollycoding.tsx +++ b/packages/mdx/src/mdx-client/scrollycoding.tsx @@ -22,14 +22,11 @@ type ScrollycodingProps = { export function Scrollycoding(props) { return ( , - ], - ["default", ], - ]} - /> + query={props.codeConfig.staticMediaQuery} + staticElement={} + > + + ) } diff --git a/packages/mdx/src/mdx-client/ssmq.tsx b/packages/mdx/src/mdx-client/ssmq.tsx index f3f2c17c..fea21a5f 100644 --- a/packages/mdx/src/mdx-client/ssmq.tsx +++ b/packages/mdx/src/mdx-client/ssmq.tsx @@ -3,6 +3,56 @@ import React from "react" let suffixCounter = 0 +const PREFERS_STATIC_KEY = "ch-prefers-static" + +export function toggleStatic() { + localStorage.setItem( + "ch-prefers-static", + localStorage.getItem("ch-prefers-static") === "true" + ? "false" + : "true" + ) + window.dispatchEvent( + new StorageEvent("storage", { + key: "ch-prefers-static", + }) + ) +} + +export function StaticToggle({ + viewDynamicText = "View dynamic version", + viewStaticText = "View static version", +}) { + const [forceStatic, toggleStatic] = useStaticToggle() + return ( + + ) +} + +export function useStaticToggle() { + const { showStatic: forceStatic } = useMedia( + "screen and (max-width: 0px)" + ) + + const [firstRender, setFirstRender] = React.useState(true) + + React.useLayoutEffect(() => { + if (forceStatic) { + setFirstRender(false) + } + }, []) + + return [ + firstRender ? false : forceStatic, + toggleStatic, + ] as const +} /** * @typedef SwapProps @@ -14,139 +64,110 @@ let suffixCounter = 0 * @param {SwapProps} props */ -export function Swap({ match }) { - const queries = match.map(([q]) => q) - const { isServer, matchedIndex } = useMedia(queries) +export function Swap({ query, staticElement, children }) { + const dynamicElement = children + + const { isServer, showStatic } = useMedia(query) const mainClassName = isServer ? "ssmq-" + suffixCounter++ : "" - return isServer ? (