2.2.0 • Published 2 years ago

react-feedbacker v2.2.0

Weekly downloads
18
License
MIT
Repository
github
Last release
2 years ago

React Feedbacker

The library does not have opinions about styles, but instead gives you the functionality required to show the feedback messages.

Visit demo

Install

npm install react-feedbacker --save

Basic Usage

import { useFeedbackContainer, DelayWrapper, feedback } from 'react-feedbacker';

const App = () => (
  <div>
    <Feedbacker />

    <button onClick={() => feedback.success('Clicked button, you have')}>
      Give feedback
    </button>
  </div>
);

const Feedbacker = () => {
  const { items, closeItem, getDelayWrapperProps } = useFeedbackContainer({
    delayCloseMs: 400,
    closeAfterMs: 4000,
  });

  return (
    <div className="FeedbackContainer">
      {items.map((item) => (
        <DelayWrapper key={item.id} {...getDelayWrapperProps({ item })}>
          <div className="FeedbackItem">
            {item.message}

            <button
              type="button"
              className="FeedbackClose"
              onClick={() => closeItem(item)}
            >
              x
            </button>
          </div>
        </DelayWrapper>
      ))}
    </div>
  );
};

Container

The container could be used either as a hook or a function. The purpose is to expose the feedback props.

useFeedbackContainer

const MyComponent = () => {
  const options = { closeAfterMs: 4000 };
  const feedbackProps = useFeedbackContainer(options);
};

FeedbackContainer

<FeedbackContainer>{props => {...}}</FeedbackContainer>
<FeedbackContainer render={props => {...}} />

Feedback Container Options

Options for the container

propertytypedescription
closeAfterMsnumber (optional), default 5000Time to wait before auto closing an item, set to 0 to disable auto close.
delayCloseMsnumber (optional), default 0Extends the time to close an item and changes the status of the item to closing after time elapsed. Allows you to for example fade out an item before it is removed from the returned items.
pauseOnHoverboolean (optional), default trueIf auto close timer should pause when mouse cursor is over a message.

Feedback Container Response

These are the returned values of either useFeedbackContainer or FeedbackContainer.

propertytypedescription
itemsFeedbackItem[]Array of items.
closeItemfunction({})A function to close an item.
getDelayWrapperPropsfunction({})A function to pass item props into DelayWrapper.

DelayWrapper

The DelayWrapper component is only required if you want to auto close items after an amount of time.

Input props

Note: these props can easily be sent to DelayWrapper component through getDelayWrapperProps provided by the Feedback Container.

<DelayWrapper {...getDelayWrapperProps({ item })}>
propertytypedescription
closeAfterMsnumberTime to wait before closing an item.
closefunction({})The close function to trigger the close of an item.
itemFeedbackItemThe item that the wrapper should use.
pauseOnHoverbooleanIf auto close timer should pause when mouse is over an item.

feedback

feedback is the way to add messages to the items returned by containers.

The default exported feedback has four methods, each setting the kind of the item:

  • success
  • warning
  • error
  • info
import { feedback } from 'react-feedbacker';

feedback.success('My message');
feedback.warning('My message');
feedback.error('My message');
feedback.info('My message');

You could optionally send a second attribute to a feedback with options.

propertytypedescription
closeAfterMsnumberOptional override of closeAfterMs
namespacestringOptional override of namespace
import { feedback } from 'react-feedbacker';

feedback.success('Some special message', {
  closeAfterMs: 0,
  namespace: 'some-namespace',
});

createFeedback

If you want to add another feedback kind, change order when adding items or want to use multiple containers through namespaces, this is the way to go.

createFeedback is a curried function according to the following

(options: FeedbackOptions) => (kind: string) => (message: ReactNode);
// We can create our own notify function by
// calling createFeedback with the options first.
const notify = createFeedback({
  namespace: 'my-namespace',
  behavior: 'prepend',
});

// In this case, we create an object with happy and sad
export const mySpecialFeedback = {
  happy: notify('success'), // kind == 'success'
  sad: notify('sad'), // kind == 'sad'
};

// Then you can use the object to show different kind of messages
mySpecialFeedback.happy('My message');

FeedbackOptions

propertydefaultdescription
namespace__DEFAULT_NAMESPACE__Specifies which which namespace or "group" the items for the feedback should insert into
behaviorappendappend or prepend. Appending will insert new items at the end of the list. Pre-pending will insert in the beginning.

FeedbackItem

Each item returned from the containers.

propertytypedescription
idstringA unique ID for each item, used for closing and deleting.
messagestringThe message sent in when creating a feedback message.
kindstring - "error", "success", "warning" or "info"The kind of item. This is always one of the types given here if the exported feedback is used.
statusstring - "open" or "closing"Status of an item. Standard is open, closing is when a close is triggered but there is a delay for delete.

Portal

Rendering the container through a portal will allow you to mount the items outside of the current mount node and into another, existing node. See the portal example or read more about portals in the react docs.

Development setup

# Installing dependencies
yarn install
# Starting the watch mode (outputs to dist)
yarn start

Build

# Make production build
yarn build

Tests

# Run tests
yarn test

License

This project is licensed under the terms of the MIT license.

2.2.0

2 years ago

2.2.0-0

2 years ago

2.1.0

4 years ago

2.0.0

5 years ago

2.0.0-0

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago