Categories
Reactstrap

Reactstrap — Modal Close Options

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add modals with Reactstrap.

Modals with a Custom Close Icon

We can change the close icon of the button with the charCode prop.

For instance, we can write:

import React, { useState } from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";

export default function App() {
  const [modal, setModal] = useState(false);

const toggle = () => setModal(!modal);

return (
    <div>
      <Button color="danger" onClick={toggle}>
        open modal
      </Button>
      <Modal isOpen={modal} toggle={toggle}>
        <ModalHeader toggle={toggle} charCode="close">
          Modal title
        </ModalHeader>
        <ModalBody>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
          eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
          minim veniam.
        </ModalBody>
        <ModalFooter>
          <Button color="primary" onClick={toggle}>
            Do Something
          </Button>{" "}
          <Button color="secondary" onClick={toggle}>
            Cancel
          </Button>
        </ModalFooter>
      </Modal>
    </div>
  );
}

We set the charCode prop to 'close' so that it’s displayed as the close button value.

Clean Up

We can change the cleanup options of our modal.

If the unmountOnClose options is false , then the data we entered into the modal stays available after we close it.

This is because it’s not completely unmounted.

For example, we can write:

import React, { useState } from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import {
  Button,
  Modal,
  ModalHeader,
  ModalBody,
  ModalFooter,
  Input,
  Form
} from "reactstrap";

export default function App() {
  const [modal, setModal] = useState(false);

  const toggle = () => setModal(!modal);

  return (
    <div>
      <Form inline onSubmit={e => e.preventDefault()}>
        <Button color="danger" onClick={toggle}>
          open modal
        </Button>
      </Form>
      <Modal isOpen={modal} toggle={toggle} unmountOnClose={false}>
        <ModalHeader toggle={toggle}>Modal title</ModalHeader>
        <ModalBody>
          <Input type="textarea" placeholder="Write something" rows={5} />
        </ModalBody>
        <ModalFooter>
          <Button color="primary" onClick={toggle}>
            Do Something
          </Button>{" "}
          <Button color="secondary" onClick={toggle}>
            Cancel
          </Button>
        </ModalFooter>
      </Modal>
    </div>
  );
}

to create a modal with a text area inside it.

When we type something, it’ll still be available after we close and open the modal again.

Focus After Close

We can make our modal focus on the button that opened it after it’s closed.

To do that, we set the returnFocusAfterClose to true .

For example, we can write:

import React, { useState } from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import {
  Button,
  Modal,
  ModalHeader,
  ModalBody,
  ModalFooter,
  Input,
  Label,
  Form,
  FormGroup
} from "reactstrap";

export default function App() {
  const [open, setOpen] = useState(false);

  const toggle = () => setOpen(!open);

  return (
    <div>
      <Button color="danger" onClick={toggle}>
        Open
      </Button>

      <Modal returnFocusAfterClose isOpen={open}>
        <ModalBody>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur
          scelerisque nisl dolor, eu aliquam nulla volutpat sed. Cras velit ex,
          aliquam quis lobortis vel, fermentum at leo. Donec hendrerit, ligula
          in ultricies suscipit, ante justo tempus erat, et cursus odio arcu ac
          lorem.
        </ModalBody>
        <ModalFooter>
          <Button color="primary" onClick={toggle}>
            Close
          </Button>
        </ModalFooter>
      </Modal>
    </div>
  );
}

Once we close the modal, the Open button will be focused.

This is because we have the returnFocusAfterClose prop added.

Conclusion

We can return focus to the element that opened the modal.

Also, the close button text and clean up options can be changed.

Categories
Reactstrap

Reactstrap — Media List and Modals

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add media lists with Reactstrap.

Media List

We can add the list prop to the Media component to make it a list.

For example, we can write:

import React from "react";
import { Media } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <Media list>
        <Media tag="li">
          <Media left href="#">
            <Media object src="http://placekitten.com/201/201" alt="cat" />
          </Media>
          <Media body>
            <Media heading>Media heading</Media>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            <Media>
              <Media left href="#">
                <Media object src="http://placekitten.com/201/201" alt="cat" />
              </Media>
              <Media body>
                <Media heading>Nested media heading</Media>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                <Media>
                  <Media left href="#">
                    <Media
                      object
                      src="http://placekitten.com/201/201"
                      alt="cat"
                    />
                  </Media>
                  <Media body>
                    <Media heading>Nested media heading</Media>
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                  </Media>
                </Media>
              </Media>
            </Media>
            <Media>
              <Media left href="#">
                <Media object src="http://placekitten.com/201/201" alt="cat" />
              </Media>
              <Media body>
                <Media heading>Nested media heading</Media>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.
              </Media>
            </Media>
          </Media>
        </Media>
        <Media tag="li">
          <Media body>
            <Media heading>Media heading</Media>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          </Media>
          <Media right href="#">
            <Media object data-src="http://placekitten.com/201/201" alt="cat" />
          </Media>
        </Media>
      </Media>
    </div>
  );
}

to add a nested list with the Media component.

tag lets us set the tag to render the component as.

The list prop lets us render the Media component as a ul element.

Modals

We can add a modal with the Modal component.

For instance, we can write:

import React from "react";
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  const [modal, setModal] = React.useState(false);

  const toggle = () => setModal(!modal);

  return (
    <div>
      <Button color="danger" onClick={toggle}>
        open modal
      </Button>
      <Modal isOpen={modal} toggle={toggle}>
        <ModalHeader toggle={toggle}>Modal title</ModalHeader>
        <ModalBody>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
          sollicitudin tortor eu velit laoreet, vitae eleifend ipsum aliquam.
          Vivamus porta est eu auctor sodales.
        </ModalBody>
        <ModalFooter>
          <Button color="primary" onClick={toggle}>
            Do Something
          </Button>
          <Button color="secondary" onClick={toggle}>
            Cancel
          </Button>
        </ModalFooter>
      </Modal>
    </div>
  );
}

We added a Button to let us open the modal.

This is done by clicking the toggle fucntion into the onClick prop.

It’ll set modal to true to open the modal.

modal is passed into the isOpen prop of the Modal to control the modal open state.

The toggle component lets us controls the toggle.

ModalHeader has the modal header.

ModalBody has the modal body.

ModalFooter has the modal footer.

Modal Backdrop

The modal backdrop can be controlled with the backdrop prop.

If we set it to false , then the backdrop is disabled.

For example, we can write:

import React from "react";
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  const [modal, setModal] = React.useState(false);

  const toggle = () => setModal(!modal);

  return (
    <div>
      <Button color="danger" onClick={toggle}>
        open modal
      </Button>
      <Modal isOpen={modal} toggle={toggle} backdrop={false}>
        <ModalHeader toggle={toggle}>Modal title</ModalHeader>
        <ModalBody>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
          sollicitudin tortor eu velit laoreet, vitae eleifend ipsum aliquam.
          Vivamus porta est eu auctor sodales.
        </ModalBody>
        <ModalFooter>
          <Button color="primary" onClick={toggle}>
            Do Something
          </Button>
          <Button color="secondary" onClick={toggle}>
            Cancel
          </Button>
        </ModalFooter>
      </Modal>
    </div>
  );
}

Since the backdrop prop is set to false , there’s no backdrop when we open the modal.

We can also set it to 'static' to disable closing by clicking away or with the Esc key.

Conclusion

We can render the Media component as a list.

Also, we can add modals with various kinds of content and options.

Categories
Reactstrap

Reactstrap — Media

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add media with Reactstrap.

Media

Media is a container that can hold images and text.

To use it, we import the Media component and write:

import React from "react";
import { Media } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <Media>
        <Media left href="#">
          <Media object src="http://placekitten.com/200/200" alt="cat" />
        </Media>
        <Media body>
          <Media heading>Media heading</Media>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
          sollicitudin tortor eu velit laoreet, vitae eleifend ipsum aliquam.
          Vivamus porta est eu auctor sodales. Aliquam et dolor vel augue tempor
          ullamcorper. Etiam velit lacus, blandit at gravida sit amet, pulvinar
          et est. Pellentesque pharetra ultricies magna non vehicula.
          Pellentesque placerat lacus ac tincidunt accumsan. Praesent turpis
          arcu, eleifend id orci vel, rhoncus lobortis magna.
        </Media>
      </Media>
    </div>
  );
}

We used the Media component as a container for content.

Also, we used it to display an image by adding the object , src and alt props.

We also used it with the body prop to display content.

The heading prop makes it a heading

Body text doesn’t need to be between any tags.

Nesting

We can nest Media components.

For example, we can write:

import React from "react";
import { Media } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <Media>
        <Media left href="#">
          <Media object src="http://placekitten.com/200/200" alt="cat" />
        </Media>
        <Media body>
          <Media heading>Media heading</Media>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
          sollicitudin tortor eu velit laoreet, vitae eleifend ipsum aliquam.
          Vivamus porta est eu auctor sodales. Aliquam et dolor vel augue tempor
          ullamcorper. Etiam velit lacus, blandit at gravida sit amet, pulvinar
          et est. Pellentesque pharetra ultricies magna non vehicula.
          Pellentesque placerat lacus ac tincidunt accumsan. Praesent turpis
          arcu, eleifend id orci vel, rhoncus lobortis magna.
          <Media>
            <Media left href="#">
              <Media object src="http://placekitten.com/201/201" cat="cat" />
            </Media>
            <Media body>
              <Media heading>Nested media heading</Media>
              Aliquam et dolor vel augue tempor ullamcorper. Etiam velit lacus,
              blandit at gravida sit amet, pulvinar et est. Pellentesque
              pharetra ultricies magna non vehicula. Pellentesque placerat lacus
              ac tincidunt accumsan. Praesent turpis arcu, eleifend id orci vel,
              rhoncus lobortis magna.
            </Media>
          </Media>
        </Media>
      </Media>
    </div>
  );
}

We nested the Media components to add an image with text side by side inside the outer Media component.

Alignment

The Media component has props to change the alignment of the media.

For example, we can write:

import React from "react";
import { Media } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <Media>
        <Media left top href="#">
          <Media object src="http://placekitten.com/200/200" alt="cat" />
        </Media>
        <Media body>
          <Media heading>Media heading</Media>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
          sollicitudin tortor eu velit laoreet, vitae eleifend ipsum aliquam.
          Vivamus porta est eu auctor sodales.
        </Media>
      </Media>
    </div>
  );
}

We added the left and top props to align the image to the top left.

Also, we can use the middle and bottom props to align the media to the middle or the bottom.

Conclusion

We can use the Media component as containers for text and images.

Categories
Reactstrap

Reactstrap — List Group Content

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add custom list group content with Reactstrap.

Custom Content

We can add custom content to our list group items.

For instance, we can write:

import React from "react";
import {
  ListGroup,
  ListGroupItem,
  ListGroupItemHeading,
  ListGroupItemText
} from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <ListGroup>
        <ListGroupItem active>
          <ListGroupItemHeading>List group item heading</ListGroupItemHeading>
          <ListGroupItemText>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          </ListGroupItemText>
        </ListGroupItem>
        <ListGroupItem>
          <ListGroupItemHeading>List group item heading</ListGroupItemHeading>
          <ListGroupItemText>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          </ListGroupItemText>
        </ListGroupItem>
        <ListGroupItem>
          <ListGroupItemHeading>List group item heading</ListGroupItemHeading>
          <ListGroupItemText>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          </ListGroupItemText>
        </ListGroupItem>
      </ListGroup>
    </div>
  );
}

We use the ListGroupItemHeading and ListGroupItemText to add our content and main text for list group items respectively.

Flush

To remove the border of the list group, we can add the flush prop to the list group.

For example, we can write:

import React from "react";
import { ListGroup, ListGroupItem } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <ListGroup flush>
        <ListGroupItem disabled tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
      </ListGroup>
    </div>
  );
}

We have the flush prop of the ListGroup to remove the border.

Horizontal List Groups

We can make the items laid out horizontally instead of vertically.

We just have to add a horizontal prop to do this.

Also, we can set the breakpoint as the value to only turn horizontal given a breakpoint.

For example, we can write:

import React from "react";
import { ListGroup, ListGroupItem } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <ListGroup horizontal>
        <ListGroupItem disabled tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
      </ListGroup>
    </div>
  );
}

to make it always horizontal.

And we can write:

import React from "react";
import { ListGroup, ListGroupItem } from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <ListGroup horizontal="lg">
        <ListGroupItem disabled tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
        <ListGroupItem tag="a" href="#">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </ListGroupItem>
      </ListGroup>
    </div>
  );
}

to make it horizontal only when the screen hits the lg breakpoint or wider.

Conclusion

We can add custom content and change the layout of list groups.

Categories
Reactstrap

Reactstrap — Layouts and List Groups

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add layouts with Reactstrap.

Container

Containers are used to hold content for layouts.

We can add them by writing:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Container } from "reactstrap";

export default function App() {
  return (
    <div>
      <Container>.container</Container>
      <Container fluid="sm">.container-sm</Container>
      <Container fluid="md">.container-md</Container>
      <Container fluid="lg">.container-lg</Container>
      <Container fluid="xl">.container-xl</Container>
      <Container fluid={true}>.container-fluid</Container>
    </div>
  );
}

We add the Container components and we can make them fluid when certain breakpoints are hit with the fluid prop.

If we specify the breakpoint, then it’ll be fluid if the screen is at the breakpoint or narrower.

If it’s true , then it’s always fluid.

Row Columns

We can add rows and columns inside containers to layout content.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Container, Row, Col } from "reactstrap";

export default function App() {
  return (
    <div>
      <Container>
        <Row xs="2">
          <Col>Column</Col>
          <Col>Column</Col>
          <Col>Column</Col>
          <Col>Column</Col>
        </Row>
        <Row xs="3">
          <Col>Column</Col>
          <Col>Column</Col>
          <Col>Column</Col>
          <Col>Column</Col>
        </Row>
      </Container>
    </div>
  );
}

We specify how many columns can be one row given the breakpoint or higher with the breakpoint prop.

So xs='2' means that we have 2 columns if the screen hits the xs breakpoint or wider.

We can also specify multiple breakpoint props:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Container, Row, Col } from "reactstrap";

export default function App() {
  return (
    <div>
      <Container>
        <Row xs="1" sm="2" md="4">
          <Col>Column</Col>
          <Col>Column</Col>
          <Col>Column</Col>
          <Col>Column</Col>
        </Row>
      </Container>
    </div>
  );
}

xs='1' means that we have 1 column per row if the breakpoint is xs or narrower.

sm='2' means that we have 2 columns per row if the breakpoint is sm or narrower.

md='4' means we have 4 columns per row if the breakpoint is md or narrow.

The props with the narrower breakpoints override the wider ones.

List Group

List groups let us add lists of items.

For example, we can use the ListGroup and ListGroupItem components as follows:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { ListGroup, ListGroupItem } from "reactstrap";

export default function App() {
  return (
    <div>
      <ListGroup>
        <ListGroupItem>Lorem ipsum dolor sit amet</ListGroupItem>
        <ListGroupItem>consectetur adipiscing elit</ListGroupItem>
        <ListGroupItem>Nunc non egestas velit</ListGroupItem>
        <ListGroupItem>sed pretium sapien</ListGroupItem>
        <ListGroupItem>Vestibulum at eros</ListGroupItem>
      </ListGroup>
    </div>
  );
}

Tags

We can add badges to display tags.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { ListGroup, ListGroupItem, Badge } from "reactstrap";

export default function App() {
  return (
    <div>
      <ListGroup>
        <ListGroupItem>
          Lorem ipsum dolor sit amet <Badge pill>11</Badge>
        </ListGroupItem>
        <ListGroupItem>
          consectetur adipiscing elit <Badge pill>12</Badge>
        </ListGroupItem>
        <ListGroupItem>
          Nunc non egestas velit <Badge pill>13</Badge>
        </ListGroupItem>
        <ListGroupItem>
          sed pretium sapien <Badge pill>14</Badge>
        </ListGroupItem>
        <ListGroupItem>
          Vestibulum at eros <Badge pill>15</Badge>
        </ListGroupItem>
      </ListGroup>
    </div>
  );
}

We use the Badge component to add tags to display them beside the text.

Disabled Items

We can make items disabled by writing:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { ListGroup, ListGroupItem } from "reactstrap";

export default function App() {
  return (
    <div>
      <ListGroup>
        <ListGroupItem disabled tag="a" href="#" `action`>
          Lorem ipsum dolor sit amet
        </ListGroupItem>
        <ListGroupItem tag="a" href="#" `action`>
          consectetur adipiscing elit
        </ListGroupItem>
        <ListGroupItem tag="a" href="#" `action`>
          Nunc non egestas velit
        </ListGroupItem>
        <ListGroupItem tag="a" href="#" `action`>
          sed pretium sapien
        </ListGroupItem>
        <ListGroupItem tag="a" href="#" `action`>
          Vestibulum at eros
        </ListGroupItem>
      </ListGroup>
    </div>
  );
}

The tag prop lets us render the list group item as an anchor element.

href is the same one for an anchor element.

The disabled prop lets us disable clicking on it.

The action prop make the buttons fit the list.

Anchors and Buttons

List group items can be rendered as buttons also.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { ListGroup, ListGroupItem } from "reactstrap";

export default function App() {
  return (
    <div>
      <ListGroup>
        <ListGroupItem disabled tag="button">
          Lorem ipsum dolor sit amet
        </ListGroupItem>
        <ListGroupItem tag="button">consectetur adipiscing elit</ListGroupItem>
        <ListGroupItem tag="button">Nunc non egestas velit</ListGroupItem>
        <ListGroupItem tag="button">sed pretium sapien</ListGroupItem>
        <ListGroupItem tag="button">Vestibulum at eros</ListGroupItem>
      </ListGroup>
    </div>
  );
}

to set the tag prop as button and render them as buttons.

Conclusion

Containers let us add layouts to our pages.

List groups let us display items in lists.