Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 class="intro">
  This was put together as a result of me having a great time learning from lesson 20 of <a href="https://javascript30.com">#javascript30</a> by <a href="https://twitter.com/wesbos">@wesbos</a>.
</div>
<div class="controls">
  <div>
    <input type="radio" name="language" value="da-DK" checked>
    <label for="male">Dansk</label>
    <input type="radio" name="language" value="es-MX">
    <label for="female">Español (MX)</label>
    <input type="radio" name="language" value="en-US">
    <label for="other">English (US)</label>
  </div>
  <div class="controls__text">
    <p><span class="say">Sig</span> <span class="trigger">skift farve</span> <span class="explain">for at skifte baggrundsfarve</span></p>
  </div>
</div>
<div class="words" contenteditable>
</div>
              
            
!

CSS

              
                :root {
  --bg: #ffc600;
}

html {
  font-size: 10px;
}

body {
  background: var(--bg);
  font-family: 'helvetica neue';
  font-weight: 200;
  font-size: 20px;
}

.intro {
  font-weight: bold;
}
.controls {
  max-width: 500px;
  margin: 50px auto;
}

.controls__text {
  margin-top: 20px;
}

.trigger {
  font-style: italic;
}

.words {
  max-width: 500px;
  margin: 50px auto;
  background: white;
  border-radius: 5px;
  box-shadow: 10px 10px 0 rgba(0, 0, 0, 0.1);
  padding: 1rem 2rem 1rem 5rem;
  background: -webkit-gradient(
      linear,
      0 0,
      0 100%,
      from(#d9eaf3),
      color-stop(4%, #fff)
    )
    0
    4px;
  background-size: 100% 3rem;
  position: relative;
  line-height: 3rem;
}

p {
  margin: 0 0 3rem;
}

.words:before {
  content: '';
  position: absolute;
  width: 4px;
  top: 0;
  left: 30px;
  bottom: 0;
  border: 1px solid;
  border-color: transparent #efe4e4;
}

              
            
!

JS

              
                window.SpeechRecognition =
  window.SpeechRecognition || window.webkitSpeechRecognition;

const recognition = new SpeechRecognition();
recognition.lang = "da-DK ";
recognition.interimResults = true;

const langStrings = {
  "da-DK": {
    say: "Sig",
    trigger: "skift farve",
    explain: "for at skifte farve"
  },
  "es-MX": {
    say: "Di",
    trigger: "cambia el color",
    explain: "para cambiar el color de fondo"
  },
  "en-US": {
    say: "Say",
    trigger: "change background",
    explain: "to change the background color"
  }
};

const controls = document.querySelector(".controls");
const say = controls.querySelector(".say");
const trigger = controls.querySelector(".trigger");
const explain = controls.querySelector(".explain");
controls.addEventListener("change", e => {
  if (!e.target.matches("input") || !e.target.checked) {
    // Not what we are listening for, so return.
    return;
  }
  // Set the language.
  recognition.lang = e.target.value;
  // Update the controls text.
  say.textContent = langStrings[e.target.value].say;
  trigger.textContent = langStrings[e.target.value].trigger;
  explain.textContent = langStrings[e.target.value].explain;
});

let p = document.createElement("p");
const words = document.querySelector(".words");
words.append(p);

function changeColor() {
  document.documentElement.style.setProperty(
    "--bg",
    `hsl(${360 * Math.random()}, ${100 * Math.random()}%, ${90 *
    Math.random()}%)`
  );
}

recognition.addEventListener("result", e => {
  const transcript = Array.from(e.results)
  .map(result => result[0])
  .map(result => result.transcript)
  .join("");
  p.textContent = transcript;
  if (e.results[0].isFinal) {
    p = document.createElement("p");
    words.appendChild(p);

    if (transcript.includes(trigger.textContent)) {
      changeColor();
    }
  }
});

recognition.addEventListener("end", e => {
  recognition.start();
});
recognition.start();

              
            
!
999px

Console