The content
property can only be used with :before
and :after
pseudo-elements and these are not available via inline CSS. Additionally, unicode characters in JavaScript strict mode must be double escaped otherwise an error is thrown because \01F680
parsed as an invalid escape sequence. To escape the \
double up \\01F680
.
Made a module that lets you write plaintext CSS in React components called Style It, will use it to demonstrate below with the HTML/CSS from CSS-Tricks.
npm install style-it --save
Emoji Toggle (OPEN IN JSFIDDLE)
import React from 'react';
import Style from 'style-it';
class Intro extends React.Component {
render() {
return Style.it(`
.emoji-toggle {
position: relative;
width: 60px;
margin: 40px auto;
}
.emoji-toggle .well {
display: block;
background: #eee;
height: 20px;
border-radius: 10px;
cursor: pointer;
}
.emoji-toggle .toggle {
opacity: 0;
border: 0;
outline: none;
height: 100%;
width: 100%;
background: transparent;
position: absolute;
cursor: pointer;
z-index: 100;
}
.emoji-toggle .toggle ~ .emoji:before {
content: "\\01F431";
position: absolute;
left: 0;
top: -15px;
font-size: 40px;
-webkit-transition: 0.2s;
transition: 0.2s;
}
.emoji-toggle .toggle:checked ~ .emoji:before {
left: 100%;
margin-left: -1em;
}
.emoji-toggle .toggle ~ label {
white-space: nowrap;
}
.emoji-toggle .toggle ~ label:before {
position: absolute;
right: 100%;
margin-right: 5px;
top: 0;
}
.emoji-toggle .toggle ~ label:after {
position: absolute;
left: 100%;
margin-left: 5px;
top: 0;
}
.emoji-travel .toggle ~ .emoji:before {
content: "✈";
}
.emoji-travel .toggle:checked ~ .emoji:before {
content: "";
}
.emoji-travel .toggle ~ label:before {
content: "Plane";
}
.emoji-travel .toggle ~ label:after {
content: "Train";
}
`,
<div className="emoji-toggle emoji-travel">
<input type="checkbox" id="toggle2" className="toggle" />
<div className="emoji" />
<label htmlFor="toggle2" className="well" />
</div>
);
}
}
ReactDOM.render(
<Intro />,
document.getElementById('container')
);
Clickable Button Emoji (OPEN IN JSFIDDLE)
Change font-size
to grow or shrink the emoji. width
, height
, and border-radius
should be double what font-size
is set to -- so if font-size: 10px;
then width: 20px; height 20px; border-radius: 20px;
.
import React from 'react';
import Style from 'style-it';
class Intro extends React.Component {
render() {
return Style.it(`
.emoji {
font-size: 20px;
height: 40px;
width: 40px;
border-radius: 40px;
text-align: center;
}
.emoji:active:before {
opacity: .7;
}
.emoji:before {
content: "\\01F431";
}
`,
<button className="emoji" />
);
}
}
ReactDOM.render(
<Intro />,
document.getElementById('container')
);
Edit: Updated and simplified per edit