npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dangerous

v0.1.21

Published

Create dangerous React components

Downloads

14

Readme

logo

☢ dangerous

npm minified size build

A utility function to create a dangerous/unsafe React component using tagged literal templates.

The syntax is borrowed from Styled Components.

dangerous returns a component, which uses dangerouslySetInnerHTML internally to convert your dangerous input to set literal to DOM's innerHTML value.

⚠️ Requirement

Minimum required version of React is v16.3.0 because dangerous uses React.forwardRef, which was introduced in v16.3.0.

Installation

$ npm i dangerous
# or
$ yarn add dangerous

✍ Usage

👶 Basic Usage

You can pass raw HTML to dangerous using tagged template literal.

const DangerousComponent = dangerous.div`💖 & 🕊`;
// or
const DangerousComponent = dangerous('div')`💖 & 🕊`

You can Subtitute div with any valid DOM elements or a custom React component.

const DangerousComponent = dangerous.span`💖 & 🕊`
const DangerousComponent = dangerous.p`💖 & 🕊`
const DangerousComponent = dangerous.section`💖 & 🕊`
// and
const DangerousComponent = dangerous(CustomComponent)`💖 & 🕊`

Render Result

Dangerous component will set &emp; directly so the rendered result will show

💖 & 🕊

while React will render it as

💖 & 🕊

as shown below.

basic-usage-render-result

🐱‍👤 Advanced Usage

dangerous returns a React component, to which you can pass props, which you can access within tagged template literal.

const DangerousComponent = dangerous.div`
  <h1>Who am I?</h1>
  <p>Last Name is "${props => props.lastName}"</p>
  <p>First Name is "${props => props.firstName}"</p>
  <a href="javascript:alert('${({ firstName, lastName }) =>
    `Hi ${firstName} ${lastName}`}');">Show Alert</a>`;

function App() {
  return <DangerousComponent firstName="Sung" lastName="Kim" />;
}

In the code above, <DangerousComponent /> is passed following props in App.

  1. firstName="Sung"
  2. lastName="Kim"

You can access the props in the tagged literal using ${props => props.properyName}.
This was taken directly from Styled Component syntax.

And you can destructure props and combine it to compose any string you want.

const DangerousComponent = dangerous.div`
  //... omitted for brevity
  <a href="javascript:alert('${({ firstName, lastName }) => `Hi ${firstName} ${lastName}`}');">Show Alert</a>`;

⤵ Return object

dangerous returns a React component and behaves like a HoC (High-order Component).

If a custom component is passed to dangerous, then all static properties will be hoisted down to the wrapped component.

👨‍💻 Example

The example below shows how to use dangerous with a custom Block components with static properties and Styled Components, StyledDangerous & StyledBlock.

It also demo's wrapping StyledBlock (a Styled Component component 😅) with dangerous as DangerousStyled.

Edit Dangerous Demo - Styled Components

import React from "react";
import ReactDOM from "react-dom";

import styled from "styled-components";
import dangerous from "dangerous";

import "./styles.css";

class Block extends React.Component {
  // To check if static fields are hoised correctly
  static count = 10;
  static increaseCount = () => console.log(++Block.count);
  static decreaseCount = () => console.log(--Block.count);

  render() {
    return <div {...this.props} />;
  }
}

const Dangerous = dangerous.div`
  <h1>Who am I?</h1>
  <p>Last Name is "${props => props.lastName}"</p>
  <p>First Name is "${props => props.firstName}"</p>
  <a href="javascript:alert('${({ firstName, lastName }) =>
    `Hi ${firstName} ${lastName}`}');">Show Alert</a>`;

const StyledDangerous = styled(Dangerous)`
  background-color: papayawhip;
  padding: 1.5em 0;
`;

const StyledBlock = styled(Block)`
  background-color: hotpink;
  padding: 1.5em 0;
`;

const DangerousStyled = dangerous(StyledBlock)`
  <h1>Alter Ego</h1>
  <p>Click link below to find out who my alter ego is</p>
  <a href="javascript:alert('${props => props.name}');">Show Aleter Ego Name</a>
`;

function App() {
  return (
    <div className="App">
      <section>
        <StyledDangerous firstName="Sung" lastName="Kim" />
        <DangerousStyled name="dance2die" />
      </section>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

🥊 Demo in action

demo

💪 To Dos

  1. Create a GitHub project for version 1.
    1. Add TypeScript types
    2. Add TypeScript definition files to distribution
    3. Add tests
  2. Update Logo to look as stylish as that of Styled Components's. 😄