+ );
}
export default App;
@@ -97,7 +95,7 @@ npm install grommet grommet-icons styled-components --save
You can now add the import of the `Grommet` component.
```diff
-import React, { Component } from 'react';
+import React from 'react';
+ import { Grommet } from 'grommet';
```
@@ -136,6 +134,7 @@ in the future by using a `theme`.
Let's now remove `plain` from Grommet and add a custom font-family, font-size, and line-height.
Below the `import` statements, let's add an initial theme declaration:
+
```javascript
const theme = {
global: {
@@ -147,7 +146,9 @@ const theme = {
},
};
```
+
And modify the Grommet tag with our new theme:
+
```javascript
```
@@ -205,29 +206,25 @@ Let's create an AppBar component and replace the `header` tag with it.
+ />
+);
-class App extends Component {
- render() {
- return (
-
--
--
+-
+- Learn React
+-
+-
++
++ Hello Grommet!
++
+
}
```
@@ -253,7 +250,7 @@ const theme = {
```
The AppBar now has a different color. You can create colors with any name; `brand` was just an example.
-Another great Grommet feature is the ability to easily declare context-aware colors which automatically adapt to light and dark themes. That is, any color can have two variations: `dark` and `light`. For example, use a light text color in a dark background,
+Another great Grommet feature is the ability to easily declare context-aware colors which automatically adapt to light and dark themes. That is, any color can have two variations: `dark` and `light`. For example, use a light text color in a dark background,
and vice-versa. We have created this [codesandbox](https://codesandbox.io/s/213mry1mnj) with more details on color usage.
## Improving the AppBar
@@ -315,59 +312,59 @@ We are extending Grommet to take the full viewport height and width. We add a Bo
Everything is so static here. Let's add some state. We are going to hide the sidebar initially and show it only when we click the notifications icon inside the AppBar.
+To start, we'll import the `useState` [React Hook](https://reactjs.org/docs/hooks-state.html).
+
```diff
-class App extends Component {
-+ state = {
-+ showSidebar: false,
-+ }
- render() {
-+ const { showSidebar } = this.state;
- return (
-
-
-
- My App
-- } onClick={() => {}} />
-+ }
-+ onClick={() => this.setState(prevState => ({ showSidebar: !prevState.showSidebar }))}
-+ />
-
-
-
- app body
-
--
-- sidebar
--
-+ {showSidebar && (
-+
-+ sidebar
-+
-+ )}
-
-
-
- );
- }
-}
+- import React from "react";
++ import React, { useState } from "react";
```
-We are just leveraging React state by creating a `showSidebar` flag initially set to false.
-Once we click in the notification button, we toggle the `showSidebar` state. The button then serves to open and
-close the sidebar.
+Then, we'll use the `useState` Hook to create a `sidebarVisible` flag initially set to `false`, and a `setSidebarVisibility` toggle that we will tie into our notification button.
+
+```diff
+function App() {
++ const [sidebarVisible, setSidebarVisibility] = useState(false)
+ return (
+
+
+
+ My App
+- } onClick={() => {}} />
++ }
++ onClick={() => setSidebarVisibility(!sidebarVisible)}
++ />
+
+
+
+ app body
+
+-
+- sidebar
+-
++ {sidebarVisible && (
++
++ sidebar
++
++ )}
+
+
+
+ );
+}
+```
## Adding Animation
@@ -381,8 +378,8 @@ Currently, the sidebar suddenly appears and disappears. Let's make it smoother b
Let's put the sidebar as a children of collapsible.
```diff
--{showSidebar && (
-+
+-{sidebarVisible && (
++ My App
}
- onClick={() => this.setState(prevState => ({ showSidebar: !prevState.showSidebar }))}
+ onClick={() => setSidebarVisibility(!sidebarVisible)}
/>
app body
--
+-
+ {size !== 'small' && (
-+
++
++ {(!sidebarVisible || size !== 'small') ? (
+
+ }
-+ onClick={() => this.setState({ showSidebar: false })}
++ onClick={() => setSidebarVisibility(!sidebarVisible)}
+ />
+ (
+const AppBar = props => (
(
/>
);
-class App extends Component {
- state = {
- showSidebar: false,
- }
- render() {
- const { showSidebar } = this.state;
- return (
-
-
- {size => (
-
-
- My App
- }
- onClick={() => this.setState({ showSidebar: !this.state.showSidebar })}
- />
-
-
-
- app body
-
- {(!showSidebar || size !== 'small') ? (
-
-
- sidebar
-
-
- ): (
-
-
- }
- onClick={() => this.setState({ showSidebar: false })}
- />
-
-
- sidebar
-
-
- )}
+function App() {
+ const [sidebarVisible, setSidebarVisibility] = useState(false);
+ return (
+
+
+ {size => (
+
+
+
+ My App
+
+ }
+ onClick={() => setSidebarVisibility(!sidebarVisible)}
+ />
+
+
+
+ app body
+ {!sidebarVisible || size !== 'small' ? (
+
+
+ sidebar
+
+
+ ) : (
+
+
+ }
+ onClick={() => setSidebarVisibility(!sidebarVisible)}
+ />
+
+
+ sidebar
+
+
+ )}
- )}
-
-
- );
- }
+
+ )}
+
+
+ );
}
export default App;