Coder Social home page Coder Social logo

soulfull / effector-react-slots Goto Github PK

View Code? Open in Web Editor NEW

This project forked from space307/effector-react-slots

0.0 1.0 0.0 1.97 MB

:comet: Effector library for slots implementation in React

License: MIT License

TypeScript 98.96% JavaScript 1.04%

effector-react-slots's Introduction

Effector React Slots

โ˜„๏ธ Effector library for slots implementation in React.

Made with love in ~/.space307

What is a slot

A slot is a place in a component where you can insert any unknown component. It's a well-known abstraction used by frameworks such as Vue.js and Svelte.

Slots aren't present in the React. With React you can achieve this goal using props or React.Context. In large projects this is not convenient, because it generates "props hell" or smears the logic.

Using React with Effector we can achieve slot goals avoiding the problems described above.

Try it out

Usage

Step 1

npm install effector-react-slots

or

yarn add effector-react-slots

Step 2

Define constant with slots name and call createSlotFactory.

import { createSlotFactory } from 'effector-react-slots';

export const SLOTS = {
  FOO: 'foo',
} as const;

export const { api, createSlot } = createSlotFactory(SLOTS);

Step 3

Create Slot component.

import { createSlot, SLOTS } from './slots';

export const { Slot: FooSlot } = createSlot(SLOTS.FOO);

Step 4

Insert Slot component to your UI.

import React from 'react';

import { FooSlot } from './fooSlot';

export const ComponentWithSlot = () => (
  <>
    <h1>Hello, Slots!</h1>
    <FooSlot />
  </>
);

Step 5

Render something inside slot. For example, based on data from feature toggle of your app.

import { split } from 'effector';

import { $featureToggle } from './featureToggle';
import { api, SLOTS } from './slots';

const MyAwesomeFeature = () => <p>Look at my horse</p>;
const VeryAwesomeFeature = () => <p>My horse is amaizing</p>;

split({
  source: $featureToggle,
  match: {
    awesome: (data) => data === 'awesome',
    veryAwesome: (data) => data === 'veryAwesome',
  },
  cases: {
    awesome: api.set.prepend(() => ({ id: SLOTS.FOO, component: MyAwesomeFeature })),
    veryAwesome: api.set.prepend(() => ({ id: SLOTS.FOO, component: VeryAwesomeFeature })),
    __: api.remove.prepend(() => ({ id: SLOTS.FOO })),
  },
});

Try it out

API

createSlotFactory

Function that returns a function for creating slots and an API for manipulating them.

const SLOTS = {
  FOO: 'foo'
};

const { createSlot, api } = createSlotFactory(SLOTS);

createSlot

Function, takes the slot id. Returns Slot component.

const { Slot } = createSlot('foo');

api.set

Method for rendering component in a slot. Takes slot id and component.

api.set({ id: 'foo', component: Foo });

api.remove

Method to stop rendering component in a slot. Takes slot id.

api.remove({ id: 'foo' });

api.hide

Allows to hide the slot data. Like api.remove, without deleting the slot data. Takes slot id.

api.hide({ id: 'foo' });

api.show

Allows to show a hidden slot data. Takes slot id.

api.show({ id: 'foo' });

api.attachLogger

Since v2.2.0

Allows to log actions that take place with slots.

api.attachLogger();

slotApi.attachLogger({
  fn: ({ meta: { slotId } }) => console.info(slotId),
});

slotApi.attachLogger({
  watchList: [SLOTS.FOO, SLOTS.BAR],
});

slotApi.attachLogger({
  watchList: [SLOTS.FOO, SLOTS.BAR],
  fn: ({ meta: { slotId } }) => console.info(slotId),
});

Arguments

fn

Optional. Function which could be used for logging. Accepts object:

{
  meta: {
    action: 'set' | 'remove' | 'hide' | 'show',
    slotId: SlotId,
    slotName: string;
  };
  message: string;
}
watchList
type: SlotId[]

Optional. Allows to specify which slots should be logged.

slotApi.attachLogger({
  watchList: [SLOTS.FOO, SLOTS.BAR],
});

Advanced

Fallback content

Since v2.1.0

Slot can contain fallback content that is rendered if no component are passed.

import { render } from 'render';
import { createSlotFactory } from 'effector-react-slots';

const SLOTS = {
  FOO: 'foo',
} as const;

const { api, createSlot } = createSlotFactory(SLOTS);
const { Slot: FooSlot } = createSlot(SLOTS.FOO);

const MyAwesomeFeature = () => <p>Look at my horse</p>;
const ComponentWithSlot = () => <FooSlot>Fallback text</FooSlot>;

render(ComponentWithSlot); // "Fallback text"
api.set({ id: SLOTS.FOO, component: MyAwesomeFeature });
render(ComponentWithSlot); // "Look at my horse"
api.remove({ id: SLOTS.FOO });
render(ComponentWithSlot); // "Fallback text"

TypeScript guide

createSlot

Props of component passed to slot can be defined as generic.

createSlot<{ readonly count: number }>(string);

Useful links

effector-react-slots's People

Contributors

binjospookie avatar inomdzhon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.