# no framework demo
online demo
# reactivity demo
reactivity demo
# vue2.x demo
in vue2.x we can write code as follow:
<!--the page often has a root element-->
<div id="app">
<div class="container" :style="{ background:backgroundColor }">
<div id="color-picker"></div>
</div>
</div>
new Vue({
data() {
return {
colorPicker: null,
backgroundColor: ""
}
},
mounted() {
const that = this;
this.colorPicker = new ewColorPicker({
el: "#color-picker",
size: "mini",
isLog: false,
alpha: true,
predefineColor: ["#fff", "#2396ef"],
sure: that.handler,
clear: that.handler
})
},
methods: {
handler(color) {
this.backgroundColor = color;
}
}
}).$mount("#app");
online demo used in vue2.x (opens new window)
# vue3.x demo
vue3.0 composition api
<div id="app">
<!-- change the data binding -->
<div class="container" :style="{ background:state.backgroundColor }">
<div id="color-picker"></div>
</div>
</div>
Vue.createApp({
setup() {
const state = Vue.reactive({
colorPicker: null,
backgroundColor: ""
});
const handler = (color) => {
state.backgroundColor = color;
}
Vue.onMounted(() => {
state.colorPicker = new ewColorPicker({
el: "#color-picker",
size: "mini",
isLog: false,
alpha: true,
predefineColor: ["#fff", "#2396ef"],
sure: handler,
clear: handler
})
})
return {
state
}
}
}).mount("#app")
online demo used in vue3.0 (opens new window)
# react demo
in react we can write code as follow:
<!--the page often has a root element-->
<div id="app"></div>
* {
margin: 0;
padding: 0;
}
.container {
padding: 20px;
background-color: #fff;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
class
class ColorPicker extends React.Component {
constructor(props){
super(props);
this.state = {
color:""
}
}
handler = (color) => {
this.setState({
color:color
})
};
componentDidMount(){
let that = this;
new ewColorPicker({
el:"#color-picker",
size:"mini",
isLog:false,
alpha:true,
predefineColor:["#fff","#2396ef"],
sure:that.handler,
clear:that.handler
});
}
render(){
return (
<div className="container" style={ {'background-color':this.state.color} }>
<div id="color-picker"></div>
</div>
)
}
}
hook
const ColorPicker = (props) => {
const [color,setColor] = React.useState("");
let handler = (color) => {
setColor(color);
};
React.useEffect(() => {
new ewColorPicker({
el:"#color-picker",
size:"mini",
isLog:false,
alpha:true,
predefineColor:["#fff","#2396ef"],
sure:handler,
clear:handler
});
return null;
},[])
return (
<div className="container" style={ {'background-color':color} }>
<div id="color-picker"></div>
</div>
)
}
then,bound it to the root element,the code as follow:
ReactDOM.render(<ColorPicker />, document.getElementById('#app'));
online demo used in react (opens new window)
# Special Note
note:The use of react such as
create-react-app
and vue such asvue-cli
is similar to introductory development。
We can also be packaged into a color component,such as vue-cli
in vue3.0
demo (opens new window)。
# more demo
see more demo (opens new window)。
# A complete example
Here is a complete example。as follow: