Breaking CSS Style of third party components
See original GitHub issueHi,
First of all, thanks for the awesome library.
I am unable to keep the style when of my sidebar when using <Can/>
. I am using antd
to create a sidebar in my app but when I try to conditionally show a menu item based on user’s ability, the styles get broken.
How can I make use of <Can/>
without breaking the css child combinator styles (.parent > .child > .child2)
?
My Code:
<Sider>
<div className="logo" />
<Menu theme="dark" mode="inline" defaultSelectedKeys={['1']}>
<Can I="view" a="applications">
<Menu.Item key="1"> // broken styles as child combinator is not found
<Icon type="app" />
<span>Applications</span>
</Menu.Item>
</Can>
<Menu.Item key="2">
<Icon type="settings" />
<span>Settings</span>
</Menu.Item>
</Menu>
</Sider>
The <li>
element generated in case of <Can/>
has class “undefined-item” instead of “ant-menu-item”, as in case of normal <Menu.Item>
.
Using the following versions:
"@casl/ability": "^3.1.2",
"@casl/react": "^1.0.3",
"react": "^16.8.6",
"antd": "^3.20.6",
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Overriding CSS properties of third-party components in Angular
You may simply provide a unique context for the CSS selector by adding a custom class to the third-party component:.
Read more >Third-Party Components at Their Best - CSS-Tricks
It should “just work” with minimal config. Lots of editable demos; Highly configurable; “White label” styling. Don't bring too strong of design ...
Read more >UX - Refresh : Angular third-party components CSS override
To override the CSS, we need to figure out the particular CSS selector, it can be class, id, element or any other context...
Read more >What is the Correct Way to Style a 3rd Party Component ...
I ran into a similar issue recently and ended up using ViewEncapsulation.None and then just writing very specific CSS selectors (e.g. #idTag >...
Read more >How to customize your third party React components
We talked about three ways to customize your third party React components. Overwriting CSS; Using Wrapper Component; Modifying the source code.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Close this as there is nothing I can do more with this. You can check my example here: https://codesandbox.io/s/admiring-field-dxc5t
After some inverstigation, there is nothing I can do with
<Can>
component to make this stuff work properly due to howrc-menu
works.rc-menu
is a lib which is used by antd for menus.That
<Menu>
goes over all children, clones them and pass additional properties. When you add<Can>
as a child of<Menu>
,<Can>
receive all props which were supposed to be passed into<Menu.Item>
. That’s why you can incorrect class and actually not only 😃 There are other state management callbacks which are passed to<Menu.Item>
from<Menu>
.So, it’s not a fault of
<React.Fragment>
. And that changes won’t help. There is potential workaround which I don’t like:What can you do to make things work?
Instead of
<Can>
component you can getAbility
instance directly from React’s context API (but keep in mind, in this case you can’t useability.update()
function. Instead you need to create a newAbility
instance each time you want to change permissions). There are 2 options:useContext
hookcontextType
I updated
@casl/react
README with that suggestions.Your code in this case will look like this: