Search code examples
reactjsgrommet

Can't include React UI frameworks (Grommet)


I have problems importing UI libraries, I had problem with ant design library so I decided to try different one, now I have problem importing Grommet.

What am I doing wrong? I added dependencies according documentation and added examples included in documentation yet still not working.

I am trying to execute this code from documentation

But it doesn't look the same at all

I work with codesandbox.io, here is link for the code on it

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";
import Heading from "grommet/components/Heading";
import Box from "grommet/components/Box";
import Header from "grommet/components/Header";
import Title from "grommet/components/Title";
import Search from "grommet/components/Search";
import Menu from "grommet/components/Menu";
import Anchor from "grommet/components/Anchor";
import "grommet-css";

class HelloWorldApp extends React.Component {
  render() {
    return (
      <div>
        <Header>
          <Title>Sample Title</Title>
          <Box flex={true} justify="end" direction="row" responsive={false}>
            <Search
              inline={true}
              fill={true}
              size="medium"
              placeHolder="Search"
              dropAlign={{right: "right"}}
            />
            <Menu dropAlign={{right: "right"}}>
              <Anchor href="#" className="active">
                First
              </Anchor>
              <Anchor href="#">Second</Anchor>
              <Anchor href="#">Third</Anchor>
            </Menu>
          </Box>
        </Header>
        <Box>
          <Heading>
            Hello, I'm a Grommet Component styled with
            <a href="https://www.npmjs.com/package/grommet-css">grommet-css</a>
          </Heading>
        </Box>
      </div>
    );
  }
}

var element = document.getElementById("root");
ReactDOM.render(<HelloWorldApp />, element);

Solution

  • So according to your code:

    <Menu dropAlign={{right: "right"}}>
    

    was missing the icon attribute, without which the Menu component directly renders the Anchor,
    the menu-items component.

    add import for the icon of your choosing, i.e: Actions ( from the example )

    import Actions from 'grommet/components/icons/base/Actions';
    

    add the icon attribute to the Menu component:

    <Menu
       icon={<Actions />}
       dropAlign={{ right: 'right' }}
    >
      <Anchor href="#" className="active">First</Anchor>
      <Anchor href="#">Second</Anchor>
      <Anchor href="#">Third</Anchor>
    </Menu>
    

    here's a codesandbox.io link which fixes your issue:
    https://codesandbox.io/s/237xo7y48p