0

In ReactJS, there is unicode table for the emojis http://apps.timwhitlock.info/emoji/tables/unicode and tried following this https://css-tricks.com/emoji-toggles/ so made the attempt to render the rocket emoji (which has unicode: U+1F680 but converted to css content: \01F680) with:

<div style={{content: '\01F680'}}></div>

But I got an error. Is this the right way to do it?

EDIT

I want to create a round/circle button with the emoji centered to the middle of it.

CSS for the button:

#emoji-button {
  border-radius: 19px;
  width: 38px;
  height: 38px;
}

And round/circle button:

<button
  type="submit"
  id="emoji-button"
/>

And is there a way to create just the emoji image itself clickable?

EDIT

enter image description here

With CSS as you suggested:

#emoji-button {
  font-size: 15px;
  width: 30px;
  height: 30px;
  border-radius: 30px;
  text-align: center;
  background-color: white;
  border:0;
  position: absolute;
  right: -60px;
}

#emoji-button:before {
  content: "\01F426";
}

3 Answers3

3

I am not sure if this is what you are looking for but i found this to be a good way to add Emoji in reactJS, especially so you don't have a ESLint fail message:

<span aria-label="a rocket blasting off" role="img"></span>

Wrap the emoji code in a span and give it a role='img' and a aria-label='description' so that it will help other developers too while they read your code. You could also add an id which you can then link to your css for styling purposes.

rh16
  • 1,056
  • 14
  • 24
Corrado
  • 29
  • 3
0

Octal literals were dropped from ECMAScript in strict mode since the 5th edition.

However, for other reasons, using '\U' before the unicode works. The problem with your case however, isn't react, or unicode. It seems unicode emojis work best when inserted as pseudo elements.

Unfortunately, you can't create inline css :before or :after pseudo elements. Your best bet would be to link an external css file, then target the div or class and define the content rules.

div:after {
  content: '\01F680';
}

Again, unicode emojis work with pseudo elements (:before or :after).

EDIT: Here's a jsfiddle that shows this idea in action.

Community
  • 1
  • 1
Cent
  • 871
  • 7
  • 16
  • @Center I tried it but still doesn't work. Could you show a working example? So I can accept the answer and upvote it. –  Sep 09 '16 at 21:24
  • Thank you it works beautifully! But a few last questions, how can I increase the size? Also how can I make it clickable so that it triggers a method (itd be really helpful like if you can show it on jfiddle again where when clicked would just console.log a string)? Lastly, what do `:before` and `:after` mean? –  Sep 09 '16 at 22:41
  • 1
    @JohnBana It is a unicode *character* so you would change the size like you would any other character, by adjusting the `font-size`. Make sure you do it in the before and after too. http://stackoverflow.com/questions/23750346/how-to-resize-unicode-characters-via-css Great picture (not to mention article to help understand pseudo elements) found here https://css-tricks.com/pseudo-element-roundup/ – Joshua Robinson Sep 09 '16 at 23:15
  • `:before` and `:after` insert something before or after a targeted elements. That's why they are pseudo elements, they aren't really there, except they are. In addition to Joshua's comment, you could also look here: http://www.w3schools.com/cssref/sel_before.asp – Cent Sep 10 '16 at 15:25
0

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

Joshua Robinson
  • 3,430
  • 1
  • 27
  • 35
  • how can I make an emoji clickable button? –  Sep 09 '16 at 23:34
  • @JohnBana See edit. Went ahead and converted the example because if pseudos/css/html are not fully groked yet then would expect the first one would be hard to pick apart. That said, do give that Chris Coyier article a read.. button conversion is more of a getting started with HTML question, recommend MDN as an awesome starter resource (use it all the time myself) https://developer.mozilla.org/en-US/docs/Learn – Joshua Robinson Sep 10 '16 at 00:22
  • Close to what I wanted to achieve! Before I accept the answer and upvote, please take a look at the original post with EDIT update as to where I want to get to. Also, in that case, how can I control the emoji size yet still have it still be centered inside the button despite the button size? Thanks again Joshua –  Sep 10 '16 at 00:45
  • @JohnBana Updated and simplified – Joshua Robinson Sep 10 '16 at 01:01
  • If I change either the size of the button or the font-size, the emoji does not stay to the center of the button though. For example, just change font-size to 30 would make the emoji be placed outside the button. Also to clarify, `.emoji:active:` clicking the button turns it into opacity of .7, correct? –  Sep 10 '16 at 01:30
  • @JohnBana If font-size is 30px then the following properties should be set width: 60px; height 60px; border-radius: 60px; to keep it centered. Yes, clicking the button changes the opacity, was just to give it an indent ion effect , could remove it – Joshua Robinson Sep 10 '16 at 03:52
  • Got it. Thank you for the clarification! So there is no way for the emoji to always stay centered relative to the button, despite the size for either? And how about if I wanted to make the emoji image itself to be the button rather than having the image on top of the button? –  Sep 10 '16 at 05:26
  • also I actually checked it again and isn't centered to the middle? –  Sep 10 '16 at 08:31
  • There is actually more space at the bottom. Is there a way to minimize the space of the button as much as possible but increase the size of the emoji image? Meaning, it will fit perfectly in it, without much of the space of the button showing. So basically only want to make the emoji image itself clickable, without much of the button space beyond the image. –  Sep 10 '16 at 11:01
  • do you see which what I am referring to? The emoji sits higher up –  Sep 10 '16 at 21:30
  • Please take a look at original post with screenshot and code EDIT. I did as you suggested but the emoji still sits up at the top. –  Sep 10 '16 at 22:12