css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!--
Source of the code: https://github.com/facebook/draft-js/blob/master/examples/draft-0-10-0/color/
-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Draft • Color</title>  
  </head>
  <body>
    <div style="font-family: Georgia, serif; font-size: 15px; padding: 20px; width: 600px; line-height: 24px;">
      This example demonstrates how custom inline styles can be used to create
      editors with an unlimited range of style combinations. Note also that
      the state of the editor can be manipulated to enforce that only one
      color may be active at any time.
    </div>
    <div id="target"></div>
  </body>
</html>
            
          
!
            
              

      const {Editor, EditorState, Modifier, RichUtils} = Draft;

      class ColorfulEditorExample extends React.Component {
        constructor(props) {
          super(props);
          this.state = {editorState: EditorState.createEmpty()};

          this.focus = () => this.refs.editor.focus();
          this.onChange = (editorState) => this.setState({editorState});
          this.toggleColor = (toggledColor) => this._toggleColor(toggledColor);
        }

        _toggleColor(toggledColor) {
          const {editorState} = this.state;
          const selection = editorState.getSelection();

          // Let's just allow one color at a time. Turn off all active colors.
          const nextContentState = Object.keys(colorStyleMap)
            .reduce((contentState, color) => {
              return Modifier.removeInlineStyle(contentState, selection, color)
            }, editorState.getCurrentContent());

          let nextEditorState = EditorState.push(
            editorState,
            nextContentState,
            'change-inline-style'
          );

          const currentStyle = editorState.getCurrentInlineStyle();

          // Unset style override for current color.
          if (selection.isCollapsed()) {
            nextEditorState = currentStyle.reduce((state, color) => {
              return RichUtils.toggleInlineStyle(state, color);
            }, nextEditorState);
          }

          // If the color is being toggled on, apply it.
          if (!currentStyle.has(toggledColor)) {
            nextEditorState = RichUtils.toggleInlineStyle(
              nextEditorState,
              toggledColor
            );
          }

          this.onChange(nextEditorState);
        }

        render() {
          const {editorState} = this.state;
          return (
            <div style={styles.root}>
              <ColorControls
                editorState={editorState}
                onToggle={this.toggleColor}
              />
              <div style={styles.editor} onClick={this.focus}>
                <Editor
                  customStyleMap={colorStyleMap}
                  editorState={editorState}
                  onChange={this.onChange}
                  placeholder="Write something colorful..."
                  ref="editor"
                />
              </div>
            </div>
          );
        }
      }

      class StyleButton extends React.Component {
        constructor(props) {
          super(props);
          this.onToggle = (e) => {
            e.preventDefault();
            this.props.onToggle(this.props.style);
          };
        }

        render() {
          let style;
          if (this.props.active) {
            style = {...styles.styleButton, ...colorStyleMap[this.props.style]};
          } else {
            style = styles.styleButton;
          }

          return (
            <span style={style} onMouseDown={this.onToggle}>
              {this.props.label}
            </span>
          );
        }
      }

      var COLORS = [
        {label: 'Red', style: 'red'},
        {label: 'Orange', style: 'orange'},
        {label: 'Yellow', style: 'yellow'},
        {label: 'Green', style: 'green'},
        {label: 'Blue', style: 'blue'},
        {label: 'Indigo', style: 'indigo'},
        {label: 'Violet', style: 'violet'},
      ];

      const ColorControls = (props) => {
        var currentStyle = props.editorState.getCurrentInlineStyle();
        return (
          <div style={styles.controls}>
            {COLORS.map(type =>
              <StyleButton
                active={currentStyle.has(type.style)}
                label={type.label}
                onToggle={props.onToggle}
                style={type.style}
              />
            )}
          </div>
        );
      };

      // This object provides the styling information for our custom color
      // styles.
      const colorStyleMap = {
        red: {
          color: 'rgba(255, 0, 0, 1.0)',
        },
        orange: {
          color: 'rgba(255, 127, 0, 1.0)',
        },
        yellow: {
          color: 'rgba(180, 180, 0, 1.0)',
        },
        green: {
          color: 'rgba(0, 180, 0, 1.0)',
        },
        blue: {
          color: 'rgba(0, 0, 255, 1.0)',
        },
        indigo: {
          color: 'rgba(75, 0, 130, 1.0)',
        },
        violet: {
          color: 'rgba(127, 0, 255, 1.0)',
        },
      };

      const styles = {
        root: {
          fontFamily: '\'Georgia\', serif',
          fontSize: 14,
          padding: 20,
          width: 600,
        },
        editor: {
          borderTop: '1px solid #ddd',
          cursor: 'text',
          fontSize: 16,
          marginTop: 20,
          minHeight: 400,
          paddingTop: 20,
        },
        controls: {
          fontFamily: '\'Helvetica\', sans-serif',
          fontSize: 14,
          marginBottom: 10,
          userSelect: 'none',
        },
        styleButton: {
          color: '#999',
          cursor: 'pointer',
          marginRight: 16,
          padding: '2px 0',
        },
      };

      ReactDOM.render(
        <ColorfulEditorExample />,
        document.getElementById('target')
      );
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console