简体   繁体   English

将 Mantine styles 覆盖为只有一个元素

[英]Override Mantine styles to only one element

In my React project, I am using Mantine's library accordion in two different components: Search.jsx and Profile.jsx .在我的 React 项目中,我在两个不同的组件中使用Mantine 的手风琴库: Search.jsx 和 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 .关键是我只需要在 Profile 中准确自定义它的 styles所以我检查了这个元素,发现默认的 Mantine 的 class 被命名为mantine-bgzycs I applied styles to that class in Profile.css and it worked as I wanted.我将 styles 应用于 Profile.css 中的 class,它按我想要的方式工作。

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.当我检查时,我还可以看到 Mantine 的默认 ID,但它会发生动态变化。 I tried to envolve the elemen with a div and apply styles but most of them are overwritten by Mantine.我尝试用 div 包含元素并应用 styles,但其中大部分被 Mantine 覆盖。

QUESTIONS:问题:

Is there a way to apply styles only to one element of the same class?有没有办法将 styles 仅应用于同一 class 的一个元素?

Is there a way to restrict styles between React components?有没有办法在 React 组件之间限制 styles?

Is there a way to access to Mantine's source and edit accurately an element styles?有没有办法访问 Mantine 的源代码并准确编辑元素 styles?

Thanks very much in advance!首先十分感谢!

There is more than one way to achieve what you're after.实现目标的方法不止一种。 My preferred approach is documented in the Styles API pane of the Accordion documentation and in the Theming documentation under create styles and Styles API我的首选方法记录在手风琴文档的Styles API 窗格和主题文档中create stylesStyles API

Here's 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>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM