最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Override Mantine styles to only one element - Stack Overflow

programmeradmin0浏览0评论

In my React project, I am using Mantine's library accordion in two different ponents: Search.jsx and Profile.jsx.

The point is I need to accurately customize its styles only in Profile so I inspected the element and discovered that the default Mantine's class is named mantine-bgzycs. I applied styles to that class in Profile.css and it worked as I wanted.

The problem is that affects to Search's accordion element too.

When I inspect I can see also a Mantine's default ID but it changes dinamically. I tried to envolve the elemen with a div and apply styles but most of them are overwritten by Mantine.

QUESTIONS:

Is there a way to apply styles only to one element of the same class?

Is there a way to restrict styles between React ponents?

Is there a way to access to Mantine's source and edit accurately an element styles?

Thanks very much in advance!

In my React project, I am using Mantine's library accordion in two different ponents: Search.jsx and Profile.jsx.

The point is I need to accurately customize its styles only in Profile so I inspected the element and discovered that the default Mantine's class is named mantine-bgzycs. I applied styles to that class in Profile.css and it worked as I wanted.

The problem is that affects to Search's accordion element too.

When I inspect I can see also a Mantine's default ID but it changes dinamically. I tried to envolve the elemen with a div and apply styles but most of them are overwritten by Mantine.

QUESTIONS:

Is there a way to apply styles only to one element of the same class?

Is there a way to restrict styles between React ponents?

Is there a way to access to Mantine's source and edit accurately an element styles?

Thanks very much in advance!

Share Improve this question edited Apr 8, 2022 at 10:24 Johann 12.4k12 gold badges67 silver badges93 bronze badges asked Apr 2, 2022 at 2:49 jmonloopjmonloop 2081 gold badge5 silver badges9 bronze badges 1
  • Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Apr 2, 2022 at 4:01
Add a ment  | 

2 Answers 2

Reset to default 4

There is more than one way to achieve what you're after. My preferred approach is documented in the Styles API panel of the Accordion documentation and in the theming documentation under create styles and Styles API

Here is an example:

import { createStyles, Accordion } from '@mantine/core';

// define custom classes - includes access to theme object
const useStyles = createStyles((theme) => ({
  item: {
    backgroundColor: "red",
  },
}));


function Demo() {
  const { classes } = useStyles();

  return (
    <Accordion
// apply custom classes to target Styles API properties
      classNames={{ item: classes.item }}>
      <Accordion.Item label="Customization">
        Blah, blah, blah
      </Accordion.Item>

    </Accordion>
  );
}

You can override object in a variable. Use MantineThemeOverride type - see docs.

For example override Text ponent:

const ponentsOveride:MantineThemeOverride["ponents"] = {
  Text: {
    styles: (theme, params, { variant }) => ({
      root:{
        fontSize: variant ==="heading" ? "32px":"16px"
      }
    }),
  },
  }


  export const theme:MantineThemeOverride = {
    ponents: ponentsOveride,
    fontFamily: "var(--font-inter)",
    colorScheme: "light",
    primaryColor: "primary",
    primaryShade: 5,
  }

App with MantineProvider:

export default function App({children}:{children: React.ReactNode}){

  return(<MantineProvider theme={theme}>{children}<?MantineProvider>)
}
发布评论

评论列表(0)

  1. 暂无评论