Git Product home page Git Product logo

rebug's Introduction

Rebug

npm version

Disclaimer: Experimental project. APIs might change, use at your discretion.

Declaratively and visually debug your component at per-component level.

Usage

Installation:

npm i -S rebug
/* Shows component display name and DOM tag name/dimensions if possible */
import { debugComponentName } from 'rebug'

class YourComponent extends React.Component{
  render(){
    return <div>My Component</div>
  }
}

/* instead of `export default YourComponent` */
export default debugComponentName(YourComponent)

Or use it as decorator:

@debugComponentName
class Component extends React.Component {
  /*...*/
}

Hover your mouse over the component to see changes!

You can customize your debug configuration:

import debug from 'rebug'
const config = {
  /* styles (currently it is the styles of wrapper component) */
  styles: {
  },
  /* styles when component is hovered. */
  hoveredStyles: {
    boxShadow: "0 0 0 1px royalblue",
  },
  /* the debug view configuration, it's visible when component is hovered */
  debugView: {
    /* Shows the component name */
    ComponentName: {
      styles: {
        background: 'royalblue',
      }
    },
    /* Shows the DOM tag name and dimensions */
    DOMTagName: {}
  }
}
export default debug(config)(YourComponent)

You can debug connected components as well:

export default debug(config)(connect(mapStateToProps, mapDispatchToProps)(SomeComponent))

Features

  • Show component's name, DOM tag name, dimensions
  • WIP: inspect component props and state
    • detect state change
  • Feature suggestions & pull requests welcome!

Implementation and known issues

The goal is to overlay (it doesn't have to be a wrapper) the debugging segment on the original component.

Currently debug is implemented as a Higher-order Component(HoC) and YourComponent is wrapped in an additional div. This might not work for some cases:

  • Need to set display property
  • some CSS selectors might not work as expected
  • Since border is rendered using box shadow, it might get blocked by the box shadow of the component to inspect.

Another approach is to ReactDOM.render the overlay component into the DOM tree on componentDidMount and ReactDOM.unmountComponentAtNode the overlay component on componentWillUnmount. In order to display the overlay at the correct position, the DOM node of the inspected component needs to be accessible. For stateless functional components, it needs to be wrapped to obtain the ref.

However, if a user wants to find the DOM node of a stateless function component, they must wrap the component in a stateful component (eg. ES6 class component) and attach the ref to the stateful wrapper component.

Also need to check the support in React DevTools API.

rebug's People

Contributors

xyc 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.