Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add ::-webkit-scrollbar pseudo element in Chakra UI element? (React)

I'm working with Chakra UI and i needed to customize the scrollbar style using the css pseudo element ::-webkit-scrollbar, but Chakra UI doesn't seen to have this pseudo element and a I don't know where I can style this specific component without creating a global css class.

Here is a sample of my code:

<Box
  overflowX="auto"
  maxW="100vw"
  h="100%"
  whiteSpace="nowrap"
  pb="17px"
  color="white"
  px="32px"
  // the ::-webkit-scrollbar property should goes here
>
  <!-- content -->
</Box>
like image 840
Gabriel Mochi Avatar asked Nov 27 '20 18:11

Gabriel Mochi


People also ask

Can I use ::- WebKit scrollbar?

Note: ::-webkit-scrollbar is only available in Blink- and WebKit-based browsers (e.g., Chrome, Edge, Opera, Safari, all browsers on iOS, and others). A standardized method of styling scrollbars is available with scrollbar-color and scrollbar-width .

How do I add a scrollbar in Reactjs?

You can apply it by placing the overflow:scroll and white-space:nowrap in the id lifestyle inside the style tag. Notice the scroll bar at the bottom and right side of the div .

How do I add a scrollbar to an element?

You need to add style="overflow-y:scroll;" to the div tag. (This will force a scrollbar on the vertical).


3 Answers

Try css prop:

<Box
  overflowY="auto"
  css={{
    '&::-webkit-scrollbar': {
      width: '4px',
    },
    '&::-webkit-scrollbar-track': {
      width: '6px',
    },
    '&::-webkit-scrollbar-thumb': {
      background: scrollbarColor,
      borderRadius: '24px',
    },
  }}
>
like image 149
nbsp Avatar answered Oct 25 '22 05:10

nbsp


Use Chakra sx prop here.

<Box
  overflowX="auto"
  maxW="100vw"
  h="100%"
  whiteSpace="nowrap"
  pb="17px"
  color="white"
  px="32px"
  sx={
     { 
    '::-webkit-scrollbar':{
           display:'none'
       }
    }
  }
>
  <!-- content -->
</Box>

To know more about Chakra sx prop, refer docs

like image 2
Abir Avatar answered Oct 25 '22 07:10

Abir


its not gonna work with just css you need to __css

<Box
  overflowY="auto"
  __css={{
        '&::-webkit-scrollbar': {
          w: '2',
        },
        '&::-webkit-scrollbar-track': {
          w: '6',
        },
        '&::-webkit-scrollbar-thumb': {
          borderRadius: '10',
          bg: `gray.100`,
        },
      }}
>
like image 1
malek Avatar answered Oct 25 '22 06:10

malek