Coder Social home page Coder Social logo

winks's Introduction

Winks.js

๐Ÿ˜œ A library made when doing assignment https://github.com/IniZio/COMP4021-SPA ๐Ÿ˜œ

Uses web-component, custom-element and html-import

Getting started

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Hello Winks.js</title>
</head>
<body>
  <!-- 0. Polyfill for web-component -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.2.0/webcomponents-lite.js"></script>
  <!-- 1. Polyfill for es5 custom-element work in modern browsers -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.2.0/custom-elements-es5-adapter.js"></script>
  <!-- 2. Winks.js -->
  <script src="https://unpkg.com/winks/dist/winks.min.js"></script>
  <!-- 3. Create a web component  -->
  <script>
    Winks.component('hello-winks', {
      template: '<h1>Hello Winks.js!</h1>'
    })
  </script>
  <!-- 4. Use the custom-element -->
  <hello-winks></hello-winks>
</body>
</html>

How to use

1. Add a new component

Winks.component('some-component', {
  template: '<h2>Component content</h2>'
})

2. Access children of the html file in its class

Winks.component('xyz-abc', {
  connectedCallback () {
    super.connectedCallback()
    console.log($(this.shadowRoot).children('#send-email'))
  }
}

3. Add event listeners to template content

Winks.component('xyz-abc', class extends MyElement {
  template: '<button @click="sendEmail">Send</button>'
  sendEmail (e) {
    e.preventDefault()
    api.sendMail()
    console.log('sending email')
  }
})

4. Use reactive state

<template>
  <input :value="message" @input="changeMessage">
  <div :children="message"></div>
</template>

<script>
  Winks.component('abc-xyz', {
    // NOTE: Use a function that returns the initial value
    data () {
      return {
        cc: 100,
        message: 'qq'
      }
    }
    changeMessage (e) {
      this.data.message = e.target.value
    }
  })
</script>

5. Use global context

<template>
  <input ~value="magic" @input="changeMessage">
  <div ~children="magic"></div>
</template>

<script>
  Winks.component('abc-xyz', {
    changeMessage (e) {
      this.context.magic = e.target.value
    }
  })
</script>

6. Use x-for directive

<template>
  <div x-for="abc:messages" :children="abc"></div>
</template>

<script>
  Winks.component('abc-xyz', {
    data () {
      return {
        messages: ['hello', 'bye', 'magic']
      }
    }
  })
</script>

7. Use x-if and x-else directive

<template>
  <button @click="toggleEdit">Edit</button>
  <input x-if="isEditting" :value="content" @input="changeContent">
  <div x-else :children="content"></div>
</template>

<script>
  Winks.component('abc-xyz', {
    data () {
      return {
        isEditting: false,
        content: ''
      }
    }
  
    toggleEdit () {
      this.data.isEditting = !this.data.isEditting
    }
  
    changeContent (e) {
      this.data.content = e.target.value
    }
  })
</script>

Development

yarn

yarn clip watch

yarn clip build

winks's People

Contributors

dependabot[bot] avatar inizio avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.