Skip to content

Commit

Permalink
Add switch
Browse files Browse the repository at this point in the history
  • Loading branch information
xinjie-zhang committed Dec 10, 2022
1 parent f2d0864 commit 002c708
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
28 changes: 28 additions & 0 deletions components/switch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template x-component:hui="switch-group">
<slot></slot>
</template>

<template x-component:hui="switch" tabindex="0" x-modelable="checked" x-data="{
checked: false,
get disabled() {return $prop('disabled')}
}" @click="$api.toggle()">
<slot></slot>
<template x-if="checked">
<input type="hidden" :name="$prop('name')" :value="$prop('value') || 'on'">
</template>
<script>
return {
toggle() {
this.checked = !this.checked
}
}
</script>
</template>

<template x-component:hui="switch-label">
<slot></slot>
</template>

<template x-component:hui="switch-description">
<slot></slot>
</template>
50 changes: 50 additions & 0 deletions examples/switch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<head>
<!--
<script src="https://unpkg.com/@vimesh/style" defer></script>
<script src="https://unpkg.com/@vimesh/ui"></script>
<script src="https://unpkg.com/alpinejs" defer></script>
-->

<script src="../node_modules/@vimesh/style/dist/vs.dev.js" defer></script>
<script src="../node_modules/@vimesh/ui/dist/vui.dev.js"></script>
<script src="../node_modules/alpinejs/dist/cdn.js" defer></script>

<script>
$vui.config.importMap = {
"*": '../components/${component}.html'
}
</script>
<style>
[x-cloak] {
display: none !important;
}
</style>

<script>
let data = {
state: true,
onSubmit() {
console.log(this.state)
}
}
</script>
</head>

<body x-cloak x-import="hui/switch" class="p-2" x-data="data">
<div class="flex h-full w-screen items-start justify-center bg-gray-50 p-12">
<form action="https://jsonplaceholder.typicode.com/todos/1">
<hui-switch-group class="flex items-center space-x-4">
<hui-switch-label>Enable notifications</hui-switch-label>

<hui-switch name="email" x-model="state" :class="['relative inline-flex flex-shrink-0 h-6 transition-colors duration-200 ease-in-out border-2 border-transparent rounded-full cursor-pointer w-11 focus:outline-none focus:shadow-outline',
checked ? 'bg-indigo-600' : 'bg-gray-200']">
<span
class="inline-block h-5 w-5 transform rounded-full bg-white transition duration-200 ease-in-out"
:class="[checked ? 'translate-x-5' : 'translate-x-0']"></span>
</hui-switch>
</hui-switch-group>
<input type="submit" value="Submit" @click="onSubmit()"
class="inline-block rounded-lg mt-2 bg-green-600 px-4 py-1.5 text-white shadow ring-1 ring-green-600 hover:bg-green-700 hover:ring-green-700">
</form>
</div>
</body>

0 comments on commit 002c708

Please sign in to comment.