Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></div>
              
            
!

CSS

              
                .app-box{
  padding-lefe:30px;
  padding-top:190px;
}
.hello-box{
  width:500px;
  border:1px solid lightblue;
  padding:6px;
}
              
            
!

JS

              
                cc.startup({
  isModuleMode: true,
  store: {
    $$default: {
      name: 'zzk of $$default',
      info: 'cc',
    },
    foo: {
      name: 'zzk of foo2',
      info: 'cc',
    },
    bar: {
      name: 'zzk of bar',
      info: 'cc',
    },
    $$global:{
      borderColor:'red',
    }
  },
  reducer: {
    foo: {
      changeName: function ({ module, dispatch, payload: name }) {
        if (module === 'foo') {
          //change bar module's state by call foo reducer module's method changeName
          dispatch('bar/foo/changeName', name);
          result = mockUploadNameToBackend(name);
          //change $$default module's state by call foo reducer module's method changeName
          dispatch('$$default/foo/changeName', result);
          //change self module(foo) state
          return { name };
        } else {
          return { name };
        }
      },
      awardYou: function ({ dispatch }) {
        const award = '恭喜你中奖500万';
        dispatch('foo/changeName', award),
          dispatch('bar/foo/changeName', award)
      },
      changeNameWithAward: function ({ module, dispatch, payload: name }) {
        console.log('changeNameWithAward', module, name);
        if (module === 'foo' && name === '666') {
          dispatch('foo/awardYou');
        } else {
          console.log('changeName');
          dispatch(`${module}/foo/changeName`, name);
        }
      }
    },
    $$global:{
      changeAppBorderColor({payload:borderColor}){
        return {borderColor};
      },
      domChangeAppBorderColor({payload}){
        //cosnt {dataset:{color:borderColor}} = payload;
        return {borderColor:payload.dataset.color};
      },
    }
  },
  computed:{
    $$global:{
      borderColor(borderColor){
         return `8px solid ${borderColor}`;
      }
    }
  }
});

function mockUploadNameToBackend(name) {
  return 'name uploaded'
}

class Hello extends React.Component {
  constructor(props) {
    super(props);
    this.state = { name: '' };
  }
  $$computed(){
    return {
      name(name){
        return name.split('').reverse().join('');
      }
    };
  }
  changeName = (e) => {
    //this.setState({name:e.currentTarget.value});
    const name = e.currentTarget.value;
    this.$$dispatch({ type: 'changeNameWithAward', reducerModule: 'foo', payload: name });
  }
  render() {
    const { name, borderColor } = this.state;
    const { name:reservedName } = this.$$refComputed;
    const toColor = this.props.toColor;
    return (
      <div className="hello-box">
        <div>{this.props.title}</div>
        <input value={name} onChange={this.changeName} />hello cc, I am <span style={{backgroundColor:'lightgray'}}>{reservedName}</span>
        <div>
          <div>
            <input placeholder="type color and enter" onKeyPress={(e)=>{
                if(e.charCode==13){
                  this.$$dispatch('$$global/changeAppBorderColor',e.currentTarget.value);
                }
              }} />
            </div>
            <hr />
            <button onClick={()=>this.$$emit('changeAppBorderColor',toColor)}>
              changeAppBorderColor by emit
            </button>
            <hr />
            <button onClick={()=>this.$$dispatch('$$global/changeAppBorderColor',toColor)}>
              changeAppBorderColor by dispatch
            </button>
            <button data-ccm="$$global" data-ccrm="$$global" data-cct="domChangeAppBorderColor" data-color={toColor} onClick={this.$$domDispatch}>
              changeAppBorderColor by domDispatch
            </button>
          <HelloFoo />
        </div>
      </div>
    )
  }
}


class Wow extends React.Component {
  constructor(props) {
    super(props);
    this.state = { name: '' };
  }
  render() {
    const { name } = this.state;
    return (
      <div className="wow-box">
        wow {name} <input value={name} onChange={(e) => this.setState({ name: e.currentTarget.value })} />
      </div>
    )
  }
}

const globalStateKeys=['borderColor'];
const CCWow = cc.register('Wow', { sharedStateKeys: '*',globalStateKeys })(Wow);
const CCHello = cc.register('Hello', { sharedStateKeys: '*',globalStateKeys  })(Hello);
const HelloFoo = cc.register('HelloFoo', { module: 'foo', sharedStateKeys: '*',globalStateKeys  })(Hello);
const HelloBar = cc.register('HelloBar', { module: 'bar', sharedStateKeys: '*',globalStateKeys })(Hello);

class App extends React.Component {
  constructor(props) {
    super(props);
  }
  componentDidMount(){
    this.$$on('changeAppBorderColor',(borderColor)=>{
       this.setGlobalState({borderColor});
    })
  }
  render() {
    const {borderColor} = this.$$globalComputed;
    return (
      <div style={{border:borderColor}} className="app-box">
        {/*<Hello title="normal instance" />*/}
        <CCHello title="cc instance1 of module $$default" toColor="gold"/>
        <CCHello title="cc instance1 of module $$default" toColor="black"/>
        <br />
        <HelloFoo title="cc instance3 of module foo" toColor="blue"/>
        <HelloFoo title="cc instance3 of module foo" toColor="purple"/>
        <br />
        <HelloBar title="cc instance3 of module bar" toColor="pink"/>
        <HelloBar title="cc instance3 of module bar" toColor="green"/>
        <br />
        <CCWow />
        <CCWow />
      </div>
    )
  }
}
const CCApp = cc.register('App',{globalStateKeys:'*'})(App);


ReactDOM.render(<CCApp />, document.getElementById('app'));
              
            
!
999px

Console