question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Breaking CSS Style of third party components

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
stalniycommented, Jul 28, 2019

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

1reaction
stalniycommented, Jul 28, 2019

After some inverstigation, there is nothing I can do with <Can> component to make this stuff work properly due to how rc-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:

<Can ...>{(isAllowed, ability, ...restProps) => <Menu.Item {...restProps}>}</Can>

What can you do to make things work?

Instead of <Can> component you can get Ability instance directly from React’s context API (but keep in mind, in this case you can’t use ability.update() function. Instead you need to create a new Ability instance each time you want to change permissions). There are 2 options:

I updated @casl/react README with that suggestions.

Your code in this case will look like this:

<Sider>
    <div className="logo" />
    <Menu theme="dark" mode="inline" defaultSelectedKeys={['1']}>
        { ability.can('view', 'applications') &&
            <Menu.Item key="1">
                <Icon type="app" />
                <span>Applications</span>
            </Menu.Item>
        }
        <Menu.Item key="2">
            <Icon type="settings" />
            <span>Settings</span>
        </Menu.Item>
    </Menu>
</Sider>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found