Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(www): fix mdx reset on package manager tab switch #6108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ptvty
Copy link

@ptvty ptvty commented Dec 17, 2024

This PR fixes #6101

The feature to switch package managers in code blocks was added in #6075

In that PR the package manager setting is added to an existing Jotai Atom which is causing the whole Mdx component to re-render and thus losing the active tab.

In this PR I used a separate Atom to fix this problem, so changing the package manager now causes a re-render in the code blocks only, keeping the tab state intact.

Copy link

vercel bot commented Dec 17, 2024

@ptvty is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

@shadcn shadcn added enhancement New feature or request area: www labels Dec 18, 2024
Copy link

vercel bot commented Dec 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
ui ✅ Ready (Inspect) Visit Preview Dec 18, 2024 9:51am

@ptvty ptvty force-pushed the fix-snippet-tab-switch branch from 473a4b5 to 9cd10c2 Compare December 19, 2024 12:28
@elijahnikov
Copy link

Was about to take a look at fixing this but saw your opened issue.

Theres no need to create a new hook, you can use useAtomValue from jotai to get a read only value from storage in apps/www/hooks/use-config.ts

export function useConfigValue<K extends keyof Config>(key: K) {
  const selectedAtom = useMemo(
    () => selectAtom(configAtom, (config) => config[key]),
    [key]
  )
  return useAtomValue(selectedAtom)
}

and then in apps/www/components/mdx-components.tsx you can write it like so:

export function Mdx({ code }: MdxProps) {
  const style = useConfigValue("style")
  const Component = useMDXComponent(code, {
    style,
  })

  return (
    <div className="mdx">
      <Component components={components} />
    </div>
  )
}

@ptvty
Copy link
Author

ptvty commented Dec 19, 2024

@elijahnikov I didn't want to touch other useConfig usages as there were plenty of them, but it seems that the only usage that is causing the re-render is the one in mdx-component.mdx.
Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: www enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug]: Component docs page resets when package manager tabs are switched in command snippet box
3 participants