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.

Box component "as" / "tag" prop not working as intended

See original GitHub issue

https://github.com/grommet/grommet/blob/bcde5d660f50359d8e46e33dcfe3bbc8968471ef/src/js/components/Box/Box.js#L75

it looks like the Box component is not using the as prop as it should. it looks to still be relying on the tag component to actually apply the styles.

using as causes the box component to become the unstyled version of itself.

examples:

screenshot 2019-01-28 16 32 24 screenshot 2019-01-28 16 32 19

when using tag it works as intended, however in the docs it says this prop is deprecated. it looks like in the code it is not used as it should be.

examples:

screenshot 2019-01-28 16 32 38 screenshot 2019-01-28 16 32 33

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
josiahdahlcommented, Apr 11, 2019

@ShimiSun @mirshko I ran into this issue today and found the explanation in an issue in the styled-components repo. https://github.com/styled-components/styled-components/issues/1981#issuecomment-419287003

Here’s the diagram from that issue

                          -> (which classes are rendered as a result)

Foo = styled.div``        -> .Foo
Bar = styled(Foo)``       -> .Bar (flat-combined styles of Foo + Bar)

<Bar as="h1" />           -> .Bar (flat-combined styles of Foo + Bar)

Foo = styled.div``        -> .Foo
Bar = () => <Foo />       -> () => .Foo
Baz = styled(Bar)``       -> .Baz .Foo (each set of styles is separate due to the custom component)

<Baz as="h1" />           -> .Baz (only the topmost styles remain since the custom component is switched out)

The way the Box component is rendered follows the second path, which I’ve simplified below, as well as a minimal example to replicate the issue.

https://github.com/grommet/grommet/blob/master/src/js/components/Box/StyledBox.js#L540
const StyledBox = styled.div`
  {props => props.prop1};
  ...
`;

// https://github.com/grommet/grommet/blob/master/src/js/components/Box/Box.js, render method
const Box = (as) => (
  <StyledBox as={as} />
);

// Custom Component
const MyBox = styled(Box)`
  border: 5px solid #f00;
};

// Broken - the `as` prop is swallowed by `styled-components` and not passed to `StyledBox`
const MyPage = () => (
  <MyBox as="header">Hello World!</MyBox>
);

// Works - The `tag` prop is not swallowed by `styled-components` and is passed 
// to `StyledBox`as the `as` prop
const MyPage2 = () => (
  <MyBox tag="header">Hello World!</MyBox>
);

The easiest solution I see is to use the tag prop for now, since I’m not experienced enough with the Grommet code base to recommend a more in-depth fix. Based on the responses from the issue in the styled-components repo, they’re not interested in fixing it, it’s a low priority, or it’s just how the library is supposed to work, all of which are their prerogative.

1reaction
CopyJoshcommented, Jun 22, 2021

I’m wondering if we should just add a note to the Box documentation for as saying that if Box is being extended with styled-components forwardedAs should be used instead

That might be worth noting, but it would have to be added to all components that use the as prop (I think*), of which there are quite a few I think.

I would actually love to see a tutorial added to the getting started with Grommet docs that explained in a best practice how to extend theming and use styled-components in general with Grommet. I know I would personally benefit greatly if someone were willing to contribute such a thing. That could include things like using this forwardAs prop.

Read more comments on GitHub >

github_iconTop Results From Across the Web

V4: Using 'as' prop on an extended styled component does not ...
Thus when you use the "as" prop with a styled() wrapper around a custom component, the custom component is no longer the thing...
Read more >
React component is not accepting JSX Element as prop
The shorthand prop for "Box" component was passed a JSX element but this slot only supports string|number|object|array|ReactElements. import ...
Read more >
Unknown Prop Warning - React
The unknown-prop warning will fire if you attempt to render a DOM element with a prop that is not recognized by React as...
Read more >
How To Customize React Components with Props
In this tutorial, you'll create custom components by passing props to your component. Props are arguments that you provide to a JSX element....
Read more >
Composition - Material UI - MUI
To solve this problem, we tag some of the components with a muiName static property when needed. You may, however, need to wrap...
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