Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                <todo-login></todo-login>
              
            
!

CSS

              
                @font-family: 'Raleway', "Helvetica Neue", Arial, sans-serif;
  @font-size: 1em;
  @color-dark: #54599c;
  @color-light: #fff;
  @color-light-gray: #d3d3d3;
  @color-light-blue: #e2f5ff;
  @color-error: #ff000e;
  @color-error-light: #fde5ec;
  @link-color: #2196F3;

  body,
  input,
  button {
    font-family: @font-family;
    font-size: @font-size;
  }

  body {
    background-color: @color-dark;
    padding: 5%;
  }

  form {
    background-color: @color-light;
    padding: 30px 40px 0 40px;
    border-radius: 6px;

  }

  input {
    border: 1px solid @color-light-gray;
    border-radius: 4px;
    padding: 3%;
    margin-bottom: 20px;

    &:not([type=checkbox]) {
      width: 93%;
    }

    &:focus {
      background-color: @color-light-blue;
      outline: 0;
      border-color: #a1c7e8;
    }
  }

  button {
    background-color: #3ec967;
    text-transform: uppercase;
    letter-spacing: 2px;
    border-radius: 20px;
    border: 0;
    color: White;
    padding: 10px;
    width: 100%;
  }

  a {
    color: @link-color;
  }

  h2 {
    color: #b027a1;
    text-align: center;
    font-size: 2em;
  }

  aside {
    background-color: #f1f0ff;
    margin: 40px -40px;
    padding: 15px;
    border-radius: 0px 0px 6px 6px;
    text-align: center;
    color: @color-dark;
  }

  .welcome-message {
    color: white;
    text-align: center;
  }

  .error {
    padding: 20px;
    margin-top: 20px;
    text-align: center;
    color: @color-error;
    background-color: @color-error-light;
  }

  todo-list {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
              
            
!

JS

              
                import {
    connect,
    connectCanSession,
    fixture,
    restModel,
    type,
    DefineMap,
    StacheElement,
  } from "//unpkg.com/can@5/ecosystem.mjs";

  setupFixtures();

  const Session = DefineMap.extend('Session', {});
  Session.connection = [
    connect.base,
    connect.dataUrl,
    connect.constructor,
    connect.canMap,
    connectCanSession
  ].reduce((conn, behavior) => behavior(conn), {
    Map: Session,
    url: {
      resource: '/api/session',
      getData: '/api/session',
      destroyData: '/api/session',
    }
  });
  Session.connection.init();

  const Todo = DefineMap.extend('Todo', {
    body: 'string',
    completed: 'boolean'
  });
  Todo.connection = restModel({
    Map: Todo,
    url: '/api/todo'
  });

  class TodoLogin extends StacheElement {
    static view = `
      {{# if (this.Session.currentPromise.isResolved) }}
        <p class="welcome-message">
           Welcome {{ Session.current.email }}.
           <a href="javascript://" on:click="this.logOut()">Log out</a>
        </p>
        <todo-list></todo-list>
      {{/ if }}

      {{# if (this.Session.currentPromise.isPending) }}
        Loading User...
      {{/ if }}

      {{# if (this.Session.currentPromise.isRejected) }}
        <form on:submit="this.logIn(scope.event)">
          <h2>Log In</h2>

          <input placeholder="email" value:to="this.email" />

          <input type="password" placeholder="password"
                 value:to="this.password" />

          <button>Log In</button>

          {{# if (this.logInError) }}
            <div class="error">{{ this.logInError.message }}</div>
          {{/ if }}

          <aside>
             Login with the following account details:<br/>
             Email: nils@bitovi.com<br/>
             Password: abc123<br/>
          </aside>
        </form>
      {{/ if }}
    `;

    static props = {
      email: String,
      password: String,
      logInError: type.Any,
    };

    get Session() {
      return Session;
    }

    logOut() {
      Session.current.destroy();
    }

    logIn(event) {
      event.preventDefault();
      this.logInError = null;

      const session = new Session({
        email: this.email,
        password: this.password
      });

      session.save().catch((error) => {
        this.logInError = error;
      });
    }
  }
  customElements.define("todo-login", TodoLogin);

  class TodoList extends StacheElement {
    static view = `
      <h3>Todos:</h3>
      <ol>
      {{#each (this.todos)}}
        <li>
          <input type="checkbox" checked:bind="this.completed" />
          {{ this.body }}
        </li>
      {{/each}}
      </ol>
    `;

    static props = {
      todos: {
        async(resolve) {
          Session.currentPromise.then(() => {
            Todo.getList({}).then(resolve);
          });
          return [];
        }
      },
    };
  }
  customElements.define("todo-list", TodoList);

  function setupFixtures() {
    fixture("GET /api/session", function (request, response) {
      const session = getSession();

      if (session) {
        response(session);
      } else {
        response(404, {message: "No session"}, {}, "unauthorized");
      }
    });

    fixture("POST /api/session", function (request, response) {
      const email = request.data.email;
      const password = request.data.password;

      if (
        email === 'nils@bitovi.com' &&
        password === 'abc123'
      ) {
        document.cookie = `SESSION-TOKEN=${btoa(JSON.stringify({email, password}))}`;
        return request.data;
      } else {
        response(401, {message: "Unauthorized"}, {}, "unauthorized");
      }
    });

    fixture("DELETE /api/session", function () {
      document.cookie = 'SESSION-TOKEN=';
      return {};
    });

    fixture("GET /api/todo", function () {
      const session = getSession();

      if (session) {
        return todos;
      } else {
        response(401, {message: "Unauthorized"}, {}, "unauthorized");
      }
    });
  }

  // get browser cookies as a map from key to value
  function getCookieMap() {
    return document.cookie.split(';').map(cookie => cookie.split('=')).reduce((map, [key, value]) => {
      map[key] = value;
      return map;
    }, {});
  }

  // get the decoded session cookie
  function getSession() {
    try {
      const token = getCookieMap()['SESSION-TOKEN'];
      // a real backend would verify the token by checking a database or by decrypting the token
      return JSON.parse(atob(token));
    } catch (e) {
      return null;
    }
  }

  const todos = [
    {body: 'buy milk', completed: true},
    {body: 'pick up dry cleaning', completed: false},
    {body: 'call mom', completed: false},
    {body: 'clean basement', completed: false},
  ];
              
            
!
999px

Console