2.1.16 • Published 8 months ago

@eliasrrosa/react-ui v2.1.16

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

react-ui

React easy to use custom components.

Introduction

The components section below will illustrate how to use each of the UI elements provided by @eliasrrosa/react-ui. To see the components working, access: https://codesandbox.io/p/sandbox/eliasrroca-react-ui-nluynm

Usage

Install with: npm i @eliasrrosa/react-ui@latest.

If you would like to import the css for the components, in any .css file your app imports, declare: @import "@eliasrrosa/react-ui"

Then import any components you would like. As in: import { FeedbackProvider } from "@eliasrrosa/react-ui"

Components

<Carousel />

See this element working at: https://codesandbox.io/p/sandbox/eliasrroca-react-ui-nluynm.

A carousel that receives {children} and renders them as in a paginated fashion. For each {children} passed there will be one extra page in the Carousel. As in:

import { Carousel } from "@eliasrrosa/react-ui";

function Child1() {
  return <p>item 1</p>;
}
function Child2() {
  return <p>item 2</p>;
}

export default function App() {
  return (
    <Carousel>
      <Child1 />
      <Child2 />
    </Carousel>
  );
}

This would be enough to have a basic carousel that can be navigated using the pagination buttons. However, if you want to manipulate the pages of the Carousel from within its {children}, you can use the following:

CarouselContext

See this element working at: https://codesandbox.io/p/sandbox/eliasrroca-react-ui-nluynm.

The {children} of <Carousel /> can consume CarouselContext through useContext() as in:

import { CarouselContext } from "@eliasrrosa/react-ui";
import { useContext } from "react";

export function Child1() {
  const carouselContext = useContext(CarouselContext);
  return <p>item 1</p>;
}

From here, you can call:

  • carouselContext.nextPage() ,
  • carouselContext.previousPage(),
  • carouselContext.goToPage(page) and
  • carouselContext.addPage()

to manipulate the <Carousel /> from within {children}.

<FeedbackProvider>

See this element working at: https://codesandbox.io/p/sandbox/eliasrroca-react-ui-nluynm.

A context for displaying error, success and loading states to the user. Wrap any elements as {children} of the <FeedbackProvider> and they will gain access to the FeedbackContext to be consumed using useContext(), from which the methods:

  • setError()
  • setSuccess()
  • setLoading()

will be available, as in:

import { FeedbackProvider, FeedbackContext } from "@eliasrrosa/react-ui";
import { useContext } from "react";

export function Child1() {
  const feedbackContext = useContext(FeedbackContext);
  return (
    <button
      className="cButton"
      onClick={() => {
        feedbackContext.setError("oops.");
      }}
    >
      setError
    </button>
  );
}

export function App() {
  return (
    <FeedbackProvider>
      <Child1 />
    </FeedbackProvider>
  );
}

It is also possible to create feedback messages outside a React Component by using the emitSuccess(), emitError() and emitLoading() functions.

<Modal> (version < 2.0.0)

See this element working at: https://codesandbox.io/p/sandbox/eliasrroca-react-ui-nluynm.

A modal with:

  • two options for position
  • opt-in background transparency
  • a context that allows the modal's {children}to control it.

<Modal> requires two props, both derived from useState(). The most basic iteration of a <Modal> is as follows:

import { Modal } from "@eliasrrosa/react-ui";
import { useState } from "react";

export function App() {
  const [isActive, setIsActive] = useState(true);
  return (
    <Modal isActive={isActive} setIsActive={setIsActive}>
      <p>Some content</p>
    </Modal>
  );
}

that would display a modal taking up the whole screen, with its content centered and a white background, with a 'close' button near the content.

However, <Modal> also offers:

  • adjustable default props
  • ModalContext, so that its {children}can manipulate it.

as in:

import { Modal, ModalContext } from "@eliasrrosa/react-ui";
import { useContext } from "react";

export function Child1() {
  const modalContext = useContext(ModalContext);
  return (
    <button
      className="cButton"
      onClick={() => {
        modalContext.setActive(false);
      }}
    >
      Close modal from child
    </button>
  );
}

export function App() {
  return (
    <Modal
      isActive={isActive}
      setIsActive={setIsActive}
      defaultAlignment={"bottom-right"}
      defaultTransparent={true}
    >
      <Child1 />
    </Modal>
  );
}

ModalContext gives you:

  • setActive()
  • setAlignment()
  • setTransparent()

Version Updates

From version 2.0.0 onwards, <Modal /> has completely changed. It implements a new API, does not include context nor receives state setters as props. This solves a variety of problems with it.

1.2.0

11 months ago

1.2.5

9 months ago

1.2.4

11 months ago

1.2.3

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago

2.1.2

8 months ago

2.1.1

8 months ago

2.1.4

8 months ago

2.1.3

8 months ago

2.1.6

8 months ago

2.1.5

8 months ago

2.1.8

8 months ago

2.1.7

8 months ago

2.1.0

8 months ago

2.0.1

9 months ago

2.0.0

9 months ago

2.1.9

8 months ago

2.1.16

8 months ago

2.1.14

8 months ago

2.1.12

8 months ago

2.1.13

8 months ago

2.1.10

8 months ago

2.1.11

8 months ago

1.1.0

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago