GithubHelp home page GithubHelp logo

remixz / react-fiction Goto Github PK

View Code? Open in Web Editor NEW
21.0 2.0 2.0 139 KB

An interactive fiction framework for React.

Home Page: http://react-fiction.bruggie.com

License: MIT License

HTML 1.55% JavaScript 93.99% CSS 4.46%

react-fiction's Introduction

react-fiction

A interactive fiction framework for React.

Installation

npm install react-fiction --save

Example

See the documentation for full usage details.

import React, { Component } from 'react'
import { render } from 'react-dom'

import {
  Story, Viewer, Rooms, Room, Passage, Link, RoomTitle, RoomComponent, ContextTypes
} from 'react-fiction'

import 'react-fiction/styles/default.css'

class Example extends Component {
  render () {
    return (
      <Story>
        <Rooms>
          <Room start id='room-1' title='Lab' component={DarkRoom} />
          <Room id='room-2' title='Lab' component={LightRoom} />
        </Rooms>

        <Viewer>
          <RoomTitle />
          <RoomComponent />
        </Viewer>
      </Story>
    )
  }
}

class DarkRoom extends Component {
  static contextTypes = {
    story: ContextTypes.StoryDataShape
  }

  render () {
    let { story } = this.context

    return (
      <div>
        <Passage>
          {story.data.turnedOnLight ? 'The room is dark again.' : 'You are in a dark room.'}
        </Passage>

        <Link to='room-2'> Turn on light </Link>
      </div>
    )
  }
}

class LightRoom extends Component {
  static contextTypes = {
    room: ContextTypes.RoomDataShape,
    story: ContextTypes.StoryDataShape
  }

  componentWillMount () {
    let { room, story } = this.context

    room.data.visited = (room.data.visited ? room.data.visited + 1 : 1)
    story.data.turnedOnLight = true

    room.updateData(room.data)
    story.updateData(story.data)
  }
  render () {
    let { room } = this.context
    return (
      <div>
        <Passage>
          The room is now illuminated. You have turned on this light {room.data.visited} times.
        </Passage>

        <Link to='room-1'> Turn off light </Link>
      </div>
    )
  }
}

render(<Example />, document.getElementById('root'))

react-fiction's People

Contributors

josephgrossberg avatar remixz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-fiction's Issues

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.