Select All Elements Inside the UI using CSS

19 replies
Last updated: Aug 5, 2022
Hi! I am trying to use a JS in my Sanity Backend.Whivh is the best option to include a small script like this for example?
Thanks

var test = document.querySelectorAll('[data-ui="Grid"]')
AI Update

I can see you're trying to run JavaScript that manipulates the DOM in Sanity Studio. The code you've shared (document.querySelectorAll('[data-ui="Grid"]')) won't work directly in Sanity's backend because Sanity Studio is a React application, and direct DOM manipulation isn't the recommended approach.

Here are your best options depending on what you're trying to achieve:

If you want to customize the Studio UI, you should create a plugin using definePlugin. This lets you add custom React components that integrate properly with Studio:

import {definePlugin} from 'sanity'
import {useEffect} from 'react'

export const myCustomPlugin = definePlugin({
  name: 'my-custom-plugin',
  studio: {
    components: {
      layout: (props) => {
        // Use React hooks like useEffect here
        useEffect(() => {
          // Your custom logic
          const grids = document.querySelectorAll('[data-ui="Grid"]')
          // Do something with grids
        }, [])
        
        return props.renderDefault(props)
      }
    }
  }
})

Then add it to your sanity.config.ts:

import {myCustomPlugin} from './plugins/myCustomPlugin'

export default defineConfig({
  // ... other config
  plugins: [myCustomPlugin()]
})

2. Custom Input Components

If you're trying to add functionality to specific fields in your documents, create a custom input component in your schema.

3. Studio Tools

If you need a separate interface or utility within Studio, you can create a custom tool that appears in the Studio navigation.

Important Notes:

  • Don't use vanilla DOM manipulation in Studio - it's a React app, so use React patterns (hooks, components, etc.)
  • useEffect is the React way to run code after components render (similar to when you'd query the DOM)
  • Studio runs in the browser, not in a "backend" - if you need actual backend logic (like responding to content changes), look into Sanity Functions instead

The plugin approach with studio.components.layout override is your best bet for running custom scripts that interact with Studio's UI elements. This gives you access to the React component lifecycle where you can safely query and manipulate DOM elements using useEffect.

What are you trying to do?
I need to select all ellements inside [data-ui=“Grid”] and add to them a custom class for example
For styling purposes or?
yes
i need to hide some items.
Have you tried following this document: https://www.sanity.io/docs/styling ?
yes , but I cant hide it with CSS. Need JS to do it 🙂
Oh, how come?
I need to hide those divs that have no child inside. Is it posible to do it with CSS?
Sure, with the
:empty
pseudo-class.
Thank you!!
Hi
user F
, I am trying to change the CSS from this div. But it is not working, any idea?
[data-ui="Grid"] [data-ui="Card"] .dINwnn{
  background: #000000 !important;
}
Did you import the stylesheet and everything as per the guide?
yes
Have you restarted your server?
I would also not rely on mangled classes (like
dINwnn
) since they’ll change with each deployment.
ohh, and how can I change the css only to this div? 😇
You could use
:last-child
maybe?
Don't be afraid to use the heck out of descendant selectors as well -- there are so many UI component containers I had to write a chain of six of them just to get the right "path" for my custom CSS.

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?