Error using Button component from sanity-ui in a studio plugin

38 replies
Last updated: Feb 11, 2021
Hi, I'm trying to use the Button component from sanity-ui in a studio plugin, but I"m getting this error:
TypeError: Cannot destructure property 'media' of 'theme.sanity' as it is undefined.
    at responsiveRadiusStyle (/studio/static/js/app.bundle.js:345740:13)
    at Ne (/studio/static/js/app.bundle.js:334335:14863)
    at e.generateAndInjectStyles (/studio/static/js/app.bundle.js:334335:11304)
    at <http://localhost:3333/studio/static/js/app.bundle.js:334335:19082>
    at <http://localhost:3333/studio/static/js/app.bundle.js:334335:19257>
    at styled.button (/studio/static/js/app.bundle.js:334335:19848)
    at renderWithHooks (/studio/static/js/vendor.bundle.js:17945:18)
    at updateForwardRef (/studio/static/js/vendor.bundle.js:19958:20)
    at beginWork (/studio/static/js/vendor.bundle.js:21787:16)
    at HTMLUnknownElement.callCallback (/studio/static/js/vendor.bundle.js:3330:14)
AI Update

This error occurs because sanity-ui components like Button require a proper theme context to function correctly. When you use sanity-ui components in a Studio plugin, they expect to be wrapped in a ThemeProvider with a Sanity theme, but your component is likely missing this context.

The error Cannot destructure property 'media' of 'theme.sanity' means the Button component is trying to access theme.sanity.media in the responsiveRadiusStyle function, but the theme object doesn't have the expected Sanity theme structure.

Quick Fix - Wrap with ThemeProvider:

import { Button, ThemeProvider, studioTheme } from '@sanity/ui'

export function MyPluginComponent() {
  return (
    <ThemeProvider theme={studioTheme}>
      <Button text="Click me" />
    </ThemeProvider>
  )
}

Why this happens:

When building Studio plugins, sanity-ui components need access to the Sanity theme context. If your component is rendered outside the Studio's normal React tree (like in a custom tool or during development), it won't have access to the theme automatically.

Common causes:

  1. Missing ThemeProvider: Your component tree doesn't have a ThemeProvider wrapping it with studioTheme

  2. Version mismatch: Your @sanity/ui version might be incompatible with your sanity version. Check your package.json and align versions.

  3. Multiple React instances: If you're developing a plugin separately and bundling it, you might have multiple React instances. When using @sanity/plugin-kit, ensure @sanity/ui, react, and styled-components are marked as external dependencies and not bundled with your plugin.

  4. Improper plugin structure: If using definePlugin, make sure your component is being rendered within the Studio's React tree where the theme context is available.

Best practice for plugins:

If you're building a proper Studio plugin, the Studio should provide the theme context automatically once your plugin is loaded. You typically shouldn't need to manually add ThemeProvider unless you're creating an isolated component tree. Make sure your plugin dependencies are configured correctly so they use the host Studio's instances of React and sanity-ui.

For background: did you update recently?
I'm actually just starting to write this plugin haha
Ah, I see 🙂
Could you try
rm -rf node_modules yarn.lock package-lock.json
, reinstall and run?
sure
within the plugin, studio, or both?
the plugin i only just kicked off a few hours ago
It depends on your workflow. Is the plugin in the
plugins/
directory or an npm module?
in plugins
OK, then reinstall in the studio 🙂
cool ok
the plugin is actually forking from a non-sanity-ui-based plugin, i'm not sure if thats a factor
i've just tried swapping the button and grid component to the sanity ui one
no dice on the reinstall
Hm, which version of
@sanity/ui
and
@sanity/base
do you have in the package.json?
the plugin i actually set up using sanistrap though, so it has its own package.json and node_modules, even inside the plugins folder
could that be wrong?
Ah, I see! I'll try that on my end as well
"@sanity/ui": "^0.33.1", in the plugin package.json
"@sanity/base": "^2.3.6" in the studio
👍
ooh got it
did a full reinstall inside the plugin folder
not sure why it made a difference given i only just created hte plugin a few hours ago haha
oh when was 0.33.1 released?
actually at 5 hours ago?
Anyhow, thanks!
Ah, sweet!
My next question already though... the buttons styling seems quite... off... any idea why that might be?
It was released earlier today 🙂
Nice timing then 😄
Can you share a screenshot?
Ah, that's a quirk with
@sanity/ui
.
Try this:


<Button text="Add row" />
<Button text="Add column" />
<Button text="Clear" />
Fwiw, the reason is that you can use the button to make custom buttons:


<Button>
  <Flex align="center" padding={3}>
    <Avatar />
    <Text>Someone</Text>
  </Flex>
</Box>
ah makes sense
ok i'll pay closer attention to the docs too haha
thanks a lot!
And with
<Grid gap={1}>
you may get some space in there 🙂
No probs!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?