React

by ken3desu

HTML

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

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

.done {
  color: rgba(0, 0, 0, 0.3);
  text-decoration: line-through;
}

input {
  margin-right: 5px;
}

React

function notify(msg, options = {}) {
  // ブラウザーが通知に対応しているかどうかをチェックしましょう
  if (!("Notification" in window)) {
    alert("このブラウザーはデスクトップ通知に対応していません。");
  }

  // 通知の許可が既に得られているかどうかをチェックしましょう
  else if (Notification.permission === "granted") {
    // If it's okay let's create a notification
    var notification = new Notification(msg, options);
  }

  // そうでなければ、ユーザーに許可を求める必要があります
  else if (Notification.permission !== "denied") {
    Notification.requestPermission().then(function (permission) {
      // ユーザーが許可したら、通知を作成しましょう
      if (permission === "granted") {
        var notification = new Notification(msg, options);
      }
    });
  }
  window.console.log(Notification.permission)

  // 最後に、ユーザーが通知を拒否していて、あなたが敬意を
  // 払いたい場合は、これ以上相手を煩わせることはありません。
}

class TodoApp extends React.Component {
  constructor(props) {
    super(props)
    this.state = {input: ''}
  }
  
  render() {
    return (
    <React.Fragment>
        <label for="not">通知メッセージ</label>
        <input type="text" id='not' value={this.state.input} onChange={(e)=>this.setState({input: e.target.value})}/>
        <button onClick={()=>notify(this.state.input)}>通知</button>
        </React.Fragment>
    )
  }
}

ReactDOM.render(<TodoApp />, document.querySelector("#app"))