Git Product home page Git Product logo

plasmic's Introduction

Plasmic

A framework for composing extensible UI applications

What are these apps built with?

From the core/types:

// At the heart your app is a visual representation of some values
export type Value = any;

// This representation leads to events, which are translated into actions
export type Action<Args extends Value[] = Value[]> = (...args: Args) => void;

// There may be other ways you want to transform these values outside of events
export type Utility<Args extends Value[] = Value[], R extends Value = void> = (
  ...args: Args
) => R;

// You put these together into shapes
export type Shape<T> = {
  [key: string]: T;
};

export type ActionShape = Shape<Action>;
export type StatusShape = Shape<Value>;
export type UtilityShape = Shape<Utility>;

// And these shapes together into features
export type Feature<
  A extends ActionShape = ActionShape,
  S extends StatusShape = StatusShape,
  U extends UtilityShape = UtilityShape
> = {
  actions: A;
  status: S;
  utilities: U;
};

// A collection of features makes a scope
export type Scope = {
  [key: string]: Feature;
};

Snippet

What does this look like in practice:

// A "Counter" feature
type CounterScope = {
  counter: {
    actions: {
      increment: (step: number) => void;
      decrement: (step: number) => void;
    };
    status: {
      count: number;
    };
    utilities: {};
  };
  log:
    actions: {};
    status: {};
    utilities: {
      log: (...string[]) =>
    }
  };
};
const { counter } = createLogicDecorators<CounterScope>({
  counter: {
    actions: ["increment", "decrement"],
    status: ["count"],
    utilities: [] as never[]
  }
});
interface CounterReducerLayer extends Layer<CounterScope> {}
interface CounterActionLayer extends Layer<CounterScope> {}
interface CounterObserverLayer extends Layer<CounterScope> {}

class CounterReducerLayer {
  @counter.on.increment.update.count
  incrementCount(current: number, step: number) {
    return current + step;
  }

  @counter.on.decrement.update.count
  decrementCount(current: number, step: number) {
    return current - step;
  }
}
class CounterActionLayer {
  @counter.on.increment.observe
  observeIncrement(previous: CounterScope["counter"]["status"], step: number) {
    console.log(
      `increment: ${previous.count} + ${step} = ${this.status.counter.count}`
    );
  }

  @counter.on.decrement.observe
  observeDecrement(previous: CounterScope["counter"]["status"], step: number) {
    console.log(
      `decrement: ${previous.count} - ${step} = ${this.status.counter.count}`
    );
  }
}
class CounterObserverLayer {
  @counter.observe
  observeCounter(previous: CounterScope["counter"]["status"]) {
    console.log(
      `counter changed: ${previous.count} => ${this.status.counter.count}`
    );
  }
}
type CounterProps = {
  step: number;
};

const Counter = composeContainerRegion<CounterScope, CounterProps>(
  ({ status, actions }, { step }) => {
    const { count } = status.counter;
    const { increment, decrement } = actions.counter;

    return (
      <React.Fragment>
        <div>{count}</div>
        <button onClick={() => increment(step)}>Up</button>
        <button onClick={() => decrement(step)}>Down</button>
      </React.Fragment>
    );
  },
  [
    new CounterReducerLayer(),
    new CounterActionLayer(),
    new CounterObserverLayer()
  ],
  {
    counter: {
      count: 0
    }
  }
);

ReactDOM.render(<Counter step={1} />, document.getElementById("root"));

plasmic's People

Contributors

alekkras avatar

Stargazers

Gábor Mihálcz avatar

Watchers

Ryan Kahn avatar James Cloos avatar Kyle Creyts avatar  avatar Cat Obuhoff avatar Richard Pickman avatar  avatar  avatar  avatar  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.