Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <todo-login></todo-login>
            
          
!
            
              @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;
  }
            
          
!
            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console