Giter VIP home page Giter VIP logo

react-jsbox's Introduction

Welcome to react-jsbox ๐Ÿ‘‹

A custom React renderer for writing JSBox apps in React.

npm: https://www.npmjs.com/package/react-jsbox

React doc: https://reactjs.org/docs/getting-started.html

JSBox doc: https://docs.xteko.com/#/en/

Example App: https://github.com/Nicify/react-jsbox-example

Examples

Class

import React from 'react'
import { render } from 'react-jsbox'

// Create React Components:
class App extends React.PureComponent {
    constructor(props) {
        super(props)
        console.log(props)
        this.state = {
            count: 0
        }
    }

    render() {
        const { width, height } = this.props
        const styles = {
            container: $rect(0, 0, width, height),
            text: $rect(0, height * 0.25 - 15, width, 30),
            list: $rect(0, height * 0.5, width, 140)
        }
        return (
            <view frame={styles.container}>
                <label
                    frame={styles.text}
                    align={$align.center}
                    font={$font('ArialRoundedMTBold', 26)}
                    text={String(this.state.count)}
                    autoFontSize={true}
                />
                <list
                    frame={styles.list}
                    scrollEnabled={false}
                    data={['INCREASE', 'DECREASE', 'RESET']}
                    events={{
                        didSelect: (sender, { row }, data) =>
                            this.setState({
                                count: this.state.count + [1, -1, -this.state.count][row]
                            })
                    }}
                />
            </view>
        )
    }
}

// Create JSBox root container and render the React component into it
$ui.render({
    props: {
        title: '',
        debugging: true
    },
    views: [
        {
            type: 'view',
            props: {
                id: 'root'
            },
            layout(make, view) {
                make.edges.equalTo(view.super.safeArea)
            },
            events: {
                layoutSubviews(view) {
                    const { width, height } = view.frame
                    render(<App width={width} height={height} />, view)
                }
            }
        }
    ]
})

Use ref to access JSBox view instance

import React from 'react'
import { render } from 'react-jsbox'

class App extends React.PureComponent {
    constructor(props) {
        super(props)
        this.state = {
            text: ''
        }
        this._input = React.createRef()
        this._handleTextChange = this.handleTextChange.bind(this)
    }

    handleTextChange(sender) {
        this.setState({ text: sender.text })
    }

    componentDidMount() {
        this._input.current.focus()
    }

    render() {
        const { width, height } = this.props
        const styles = {
            container: $rect(0, 0, width, height - 40),
            text: $rect(0, 64, width, 30),
            textInput: $rect(10, 160, width - 20, 48)
        }
        return (
            <view
                id="container"
                frame={styles.container}
                bgcolor={$color('#2ac')}
                events={{
                    tapped: () => this._input.current.blur()
                }}
            >
                <label
                    id={'text'}
                    frame={styles.text}
                    align={$align.center}
                    font={$font('ArialRoundedMTBold', 26)}
                    text={this.state.text || 'Hello World!'}
                    textColor={$color('#fff')}
                    autoFontSize={true}
                />
                <input
                    id={'textInput'}
                    // use ref to access JSBox view instance
                    ref={this._input}
                    frame={styles.textInput}
                    font={$font('ArialRoundedMTBold', 24)}
                    tintColor={$color('orange')}
                    placeholder={'Type here...'}
                    events={{
                        changed: this._handleTextChange
                    }}
                />
            </view>
        )
    }
}

$ui.render({
    props: {
        title: '',
        debugging: true
    },
    views: [
        {
            type: 'view',
            props: {
                id: 'root'
            },
            layout(make, view) {
                make.edges.equalTo(view.super.safeArea)
            },
            events: {
                layoutSubviews(view) {
                    const { width, height } = view.frame
                    render(<App width={width} height={height} />, view)
                }
            }
        }
    ]
})

Hooks

useReducer

import React, { useMemo } from 'react'
import { render } from 'react-jsbox'

function App({ width, height }) {
    const [state, dispatch] = React.useReducer(counterReducer, { count: 0 })

    const styles = useMemo(
        () => ({
            container: $rect(0, 0, width, height),
            text: $rect(0, height * 0.25 - 15, width, 30),
            list: $rect(0, height * 0.5, width, 140)
        }),
        [width, height]
    )

    return (
        <view frame={styles.container}>
            <label
                frame={styles.text}
                align={$align.center}
                font={$font('ArialRoundedMTBold', 26)}
                text={String(state.count)}
                autoFontSize={true}
            />
            <list
                frame={styles.list}
                scrollEnabled={false}
                data={['INCREASE', 'DECREASE', 'RESET']}
                events={{
                    didSelect: (sender, indexPath, data) => dispatch({ type: data })
                }}
            />
        </view>
    )
}

const counterReducer = (state, action) => {
    switch (action.type) {
        case 'INCREASE':
            return { ...state, count: state.count + 1 }
        case 'DECREASE':
            return { ...state, count: state.count - 1 }
        case 'RESET':
            return { ...state, count: 0 }
        default:
            throw new Error()
    }
}

$ui.render({
    props: {
        title: '',
        debugging: true
    },
    views: [
        {
            type: 'view',
            props: {
                id: 'root'
            },
            layout(make, view) {
                make.edges.equalTo(view.super.safeArea)
            },
            events: {
                layoutSubviews(view) {
                    const { width, height } = view.frame
                    render(<App width={width} height={height} />, view)
                }
            }
        }
    ]
})

useEffect

In useCache.js

import { useEffect, useState } from 'react'

const useCache = (key, initialValue) => {
    const [state, setState] = useState(() => {
        const cacheValue = $cache.get(key)
        if (cacheValue === undefined) {
            $cache.set(key, initialValue)
            return initialValue
        }
        return cacheValue
    })
    useEffect(() => $cache.set(key, state))

    return [state, setState]
}

export default useCache

In app.js

import React, { useMemo } from 'react'
import { render, useCache } from 'react-jsbox'
import useCache from './useCache'

function App({ width, height }) {
    const [count, setCount] = useCache('count', 0)

    const styles = useMemo(
        () => ({
            container: $rect(0, 0, width, height),
            text: $rect(0, height * 0.25 - 15, width, 30),
            list: $rect(0, height * 0.5, width, 140)
        }),
        [width, height]
    )

    return (
        <view frame={styles.container}>
            <label
                frame={styles.text}
                align={$align.center}
                font={$font('ArialRoundedMTBold', 26)}
                text={String(count)}
                autoFontSize={true}
            />
            <list
                frame={styles.list}
                scrollEnabled={false}
                data={['INCREASE', 'DECREASE', 'RESET']}
                events={{
                    didSelect(sender, { row }) {
                        setCount(x => x + [1, -1, -x][row])
                    }
                }}
            />
        </view>
    )
}

$ui.render({
    props: {
        title: '',
        debugging: true
    },
    views: [
        {
            type: 'view',
            props: {
                id: 'root'
            },
            layout(make, view) {
                make.edges.equalTo(view.super.safeArea)
            },
            events: {
                layoutSubviews(view) {
                    const { width, height } = view.frame
                    render(<App width={width} height={height} />, view)
                }
            }
        }
    ]
})

JsxLiteral

const htm = require('htm')
const { createElement, useState, useMemo } = require('react')
const ReactJSBox = require('react-jsbox')
const jsx = htm.bind(createElement)

function JsxLiteralExample({ width, height }) {
    const [count, setCount] = useState(0)

    const styles = useMemo(
        () => ({
            container: $rect(0, 0, width, height),
            text: $rect(0, height * 0.25 - 15, width, 30),
            list: $rect(0, height * 0.5, width, 140)
        }),
        [width, height]
    )

    return jsx`<view frame=${styles.container}>
      <label
        frame=${styles.text}
        align=${$align.center}
        font=${$font(26)}
        text=${String(count)}
        autoFontSize=${true}
      />
      <list
        frame=${styles.list}
        scrollEnabled=${false}
        data=${['INCREASE', 'DECREASE', 'RESET']}
        events=${{
            didSelect: (sender, { row }) => {
                setCount(x => x + [1, -1, -x][row])
            }
        }}
      />
    </view>`
}

$ui.render({
    props: {
        title: 'JsxLiteralExample'
    },
    views: [
        {
            type: 'view',
            props: {
                id: 'root'
            },
            layout(make, view) {
                make.edges.equalTo(view.super.safeArea)
            },
            events: {
                layoutSubviews(view) {
                    const { width, height } = view.frame
                    ReactJSBox.render(jsx`<${JsxLiteralExample} width=${width} height=${height} />`, view)
                }
            }
        }
    ]
})

Author

๐Ÿ‘ค Eva1ent

Show your support

Give a โญ๏ธ if this project helped you !

react-jsbox's People

Contributors

dependabot-preview[bot] avatar rel1cx avatar renovate-bot avatar

Stargazers

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

Watchers

 avatar  avatar

Forkers

linecode

react-jsbox's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • react-reconciler 0.25.1
  • @babel/core 7.16.7
  • @babel/plugin-proposal-class-properties 7.16.7
  • @babel/plugin-transform-runtime 7.16.7
  • @babel/preset-env 7.16.7
  • @babel/preset-react 7.16.7
  • @babel/runtime 7.16.7
  • @babel/eslint-parser 7.16.5
  • babel-preset-minify 0.5.1
  • eslint 8.6.0
  • eslint-plugin-babel 5.3.1
  • eslint-plugin-import 2.25.3
  • eslint-plugin-jsbox 0.1.1
  • eslint-plugin-react-hooks 4.3.0
  • prettier 2.5.1
  • react 17.0.2
  • rollup 2.62.0
  • rollup-plugin-babel 4.4.0
  • rollup-plugin-cleanup 3.2.1
  • rollup-plugin-commonjs 10.1.0
  • rollup-plugin-modify 3.0.0
  • rollup-plugin-node-resolve 5.2.0
  • rollup-plugin-progress 1.1.2
  • rollup-plugin-terser 7.0.2
  • react ^17.0.2

  • Check this box to trigger a request for Renovate to run again on this repository

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.