Skip to content

Commit

Permalink
initial <Switch> component
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinJoyce committed Oct 2, 2020
1 parent a9c3e13 commit f2a9147
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 2 deletions.
18 changes: 18 additions & 0 deletions addon/components/switch.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div ...attributes>
{{yield (hash
isChecked=@isChecked
Label=(
component 'switch/label'
guid=this.labelGuid
onPointerUp=this.onLabelPointerUp
)
Button=(
component 'switch/button'
guid=this.buttonGuid
labelGuid=this.labelGuid
isChecked=@isChecked
onClick=this.onButtonClick
onKeyUp=this.onButtonKeyUp
)
)}}
</div>
35 changes: 35 additions & 0 deletions addon/components/switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Component from "@glimmer/component";
import { action } from "@ember/object";
import { guidFor } from "@ember/object/internals";

export default class Switch extends Component {
guid = `${guidFor(this)}-tailwindui-switch`;
labelGuid = `${this.guid}-label`;
buttonGuid = `${this.guid}-button`;

@action
onLabelPointerUp() {
let buttonElement = document.getElementById(this.buttonGuid);
buttonElement.click();
buttonElement.focus();
}

@action
onButtonClick(event) {
event.preventDefault();
this._toggle();
}

@action
onButtonKeyUp(event) {
event.preventDefault();
if (event.key === " ") {
event.preventDefault();
this._toggle();
}
}

_toggle() {
this.args.onUpdate(!this.args.isChecked);
}
}
12 changes: 12 additions & 0 deletions addon/components/switch/button.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id={{@guid}}
role="switch"
tabindex="0"
aria-checked={{@isChecked}}
aria-labelledby={{@labelGuid}}
{{on "click" @onClick}}
{{on "keyup" @onKeyUp}}
...attributes
>
{{yield}}
</div>
7 changes: 7 additions & 0 deletions addon/components/switch/label.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<label
id={{@guid}}
{{on "pointerup" @onPointerUp}}
...attributes
>
{{yield}}
</label>
1 change: 1 addition & 0 deletions app/components/switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-headlessui/components/switch";
1 change: 1 addition & 0 deletions app/components/switch/button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-headlessui/components/switch/button";
1 change: 1 addition & 0 deletions app/components/switch/label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-headlessui/components/switch/label";
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"ember-on-helper": "^0.1.0",
"ember-qunit": "^4.6.0",
"ember-resolver": "^8.0.2",
"ember-simple-set-helper": "^0.1.2",
"ember-source": "~3.21.1",
"ember-source-channel-url": "^3.0.0",
"ember-template-lint": "^2.11.0",
Expand Down
4 changes: 4 additions & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ Router.map(function () {
this.route("menu-with-transition");
this.route("menu-with-transition-and-popper");
});

this.route("switch", function () {
this.route("switch-basic");
});
});
8 changes: 8 additions & 0 deletions tests/dummy/app/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
</li>
</ul>
</li>
<li>
<h3 class="text-xl">Switch</h3>
<ul>
<li>
<a href="/switch/switch-basic">Switch (basic)</a>
</li>
</ul>
</li>
</ul>

</div>
Expand Down
22 changes: 22 additions & 0 deletions tests/dummy/app/templates/switch/switch-basic.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

<div class="flex items-start justify-center w-screen h-full p-12 bg-gray-50">
<Switch
@isChecked={{this.isChecked}}
@onUpdate={{set this.isChecked}}
class="flex items-center space-x-4"
as |switch|
>
<switch.Label>Enable notifications</switch.Label>
<switch.Button
class="
relative inline-flex flex-shrink-0 h-6 border-2 border-transparent rounded-full cursor-pointer w-11 focus:outline-none focus:shadow-outline transition-colors ease-in-out duration-200
{{if switch.isChecked "bg-indigo-600" "bg-gray-200"}}
"
>
<span class="
inline-block w-5 h-5 transition duration-200 ease-in-out transform bg-white rounded-full
{{if switch.isChecked "translate-x-5" "translate-x-0"}}
" />
</switch.Button>
</Switch>
</div>
57 changes: 55 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2113,6 +2113,11 @@ babel-plugin-htmlbars-inline-precompile@^1.0.0:
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-1.0.0.tgz#a9d2f6eaad8a3f3d361602de593a8cbef8179c22"
integrity sha512-4jvKEHR1bAX03hBDZ94IXsYCj3bwk9vYsn6ux6JZNL2U5pvzCWjqyrGahfsGNrhERyxw8IqcirOi9Q6WCo3dkQ==

babel-plugin-htmlbars-inline-precompile@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-3.2.0.tgz#c4882ea875d0f5683f0d91c1f72e29a4f14b5606"
integrity sha512-IUeZmgs9tMUGXYu1vfke5I18yYJFldFGdNFQOWslXTnDWXzpwPih7QFduUqvT+awDpDuNtXpdt5JAf43Q1Hhzg==

babel-plugin-htmlbars-inline-precompile@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-4.2.0.tgz#73e7a199c14db139b9c9aea240e03b7112784c81"
Expand Down Expand Up @@ -3011,6 +3016,13 @@ broccoli-node-info@^2.1.0:
resolved "https://registry.yarnpkg.com/broccoli-node-info/-/broccoli-node-info-2.1.0.tgz#ca84560e8570ff78565bea1699866ddbf58ad644"
integrity sha512-l6qDuboJThHfRVVWQVaTs++bFdrFTP0gJXgsWenczc1PavRVUmL1Eyb2swTAXXMpDOnr2zhNOBLx4w9AxkqbPQ==

broccoli-output-wrapper@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/broccoli-output-wrapper/-/broccoli-output-wrapper-2.0.0.tgz#f1e0b9b2f259a67fd41a380141c3c20b096828e6"
integrity sha512-V/ozejo+snzNf75i/a6iTmp71k+rlvqjE3+jYfimuMwR1tjNNRdtfno+NGNQB2An9bIAeqZnKhMDurAznHAdtA==
dependencies:
heimdalljs-logger "^0.1.10"

broccoli-output-wrapper@^3.2.1:
version "3.2.3"
resolved "https://registry.yarnpkg.com/broccoli-output-wrapper/-/broccoli-output-wrapper-3.2.3.tgz#e5c9de7c881570eb4c0b0d194bb12d9671b25a9b"
Expand Down Expand Up @@ -3039,7 +3051,7 @@ broccoli-persistent-filter@^1.1.6, broccoli-persistent-filter@^1.4.3:
symlink-or-copy "^1.0.1"
walk-sync "^0.3.1"

broccoli-persistent-filter@^2.1.0, broccoli-persistent-filter@^2.2.1, broccoli-persistent-filter@^2.2.2, broccoli-persistent-filter@^2.3.0:
broccoli-persistent-filter@^2.1.0, broccoli-persistent-filter@^2.2.1, broccoli-persistent-filter@^2.2.2, broccoli-persistent-filter@^2.3.0, broccoli-persistent-filter@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/broccoli-persistent-filter/-/broccoli-persistent-filter-2.3.1.tgz#4a052e0e0868b344c3a2977e35a3d497aa9eca72"
integrity sha512-hVsmIgCDrl2NFM+3Gs4Cr2TA6UPaIZip99hN8mtkaUPgM8UeVnCbxelCvBjUBHo0oaaqP5jzqqnRVvb568Yu5g==
Expand Down Expand Up @@ -3106,6 +3118,19 @@ broccoli-plugin@^2.1.0:
rimraf "^2.3.4"
symlink-or-copy "^1.1.8"

broccoli-plugin@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/broccoli-plugin/-/broccoli-plugin-3.1.0.tgz#54ba6dd90a42ec3db5624063292610e326b1e542"
integrity sha512-7w7FP8WJYjLvb0eaw27LO678TGGaom++49O1VYIuzjhXjK5kn2+AMlDm7CaUFw4F7CLGoVQeZ84d8gICMJa4lA==
dependencies:
broccoli-node-api "^1.6.0"
broccoli-output-wrapper "^2.0.0"
fs-merger "^3.0.1"
promise-map-series "^0.2.1"
quick-temp "^0.1.3"
rimraf "^2.3.4"
symlink-or-copy "^1.1.8"

broccoli-plugin@^4.0.1, broccoli-plugin@^4.0.2, broccoli-plugin@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/broccoli-plugin/-/broccoli-plugin-4.0.3.tgz#9dcfbfb6a1b27a37cc22e65c071719ce9f92bc1e"
Expand Down Expand Up @@ -4582,6 +4607,26 @@ ember-cli-htmlbars-inline-precompile@^2.1.0:
heimdalljs-logger "^0.1.9"
silent-error "^1.1.0"

ember-cli-htmlbars@^4.2.2:
version "4.4.0"
resolved "https://registry.yarnpkg.com/ember-cli-htmlbars/-/ember-cli-htmlbars-4.4.0.tgz#7ca17d5ca8f7550984346d9e6e93da0c3323f8d9"
integrity sha512-ohgctqk7dXIZR4TgN0xRoUYltWhghFJgqmtuswQTpZ7p74RxI9PKx+E8WV/95mGcPzraesvMNBg5utQNvcqgNg==
dependencies:
"@ember/edition-utils" "^1.2.0"
babel-plugin-htmlbars-inline-precompile "^3.2.0"
broccoli-debug "^0.6.5"
broccoli-persistent-filter "^2.3.1"
broccoli-plugin "^3.1.0"
common-tags "^1.8.0"
ember-cli-babel-plugin-helpers "^1.1.0"
fs-tree-diff "^2.0.1"
hash-for-dep "^1.5.1"
heimdalljs-logger "^0.1.10"
json-stable-stringify "^1.0.1"
semver "^6.3.0"
strip-bom "^4.0.0"
walk-sync "^2.0.2"

ember-cli-htmlbars@^5.1.0, ember-cli-htmlbars@^5.2.0, ember-cli-htmlbars@^5.3.1:
version "5.3.1"
resolved "https://registry.yarnpkg.com/ember-cli-htmlbars/-/ember-cli-htmlbars-5.3.1.tgz#61793964fc2599ce750db9e972ab55c6dd177c48"
Expand Down Expand Up @@ -5010,6 +5055,14 @@ ember-router-generator@^2.0.0:
"@babel/traverse" "^7.4.5"
recast "^0.18.1"

ember-simple-set-helper@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/ember-simple-set-helper/-/ember-simple-set-helper-0.1.2.tgz#f0aad11f35df5f0396561ee4552c1790d8ef0930"
integrity sha512-Dx66Ki7tqd3R0MpAmE1j1pTI/K5OAcW4XYpz0T23x8KVUWbaZE9zGGUPlxsczf/44ML3kyIvS7rzQ+6DHS7dkw==
dependencies:
ember-cli-babel "^7.17.2"
ember-cli-htmlbars "^4.2.2"

ember-source-channel-url@^1.0.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/ember-source-channel-url/-/ember-source-channel-url-1.2.0.tgz#77eb9d0889e5f5370e6c70fcb2696c63ff4a34a1"
Expand Down Expand Up @@ -6138,7 +6191,7 @@ fs-extra@^9.0.0:
jsonfile "^6.0.1"
universalify "^1.0.0"

fs-merger@^3.1.0:
fs-merger@^3.0.1, fs-merger@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/fs-merger/-/fs-merger-3.1.0.tgz#f30f74f6c70b2ff7333ec074f3d2f22298152f3b"
integrity sha512-RZ9JtqugaE8Rkt7idO5NSwcxEGSDZpLmVFjtVQUm3f+bWun7JAU6fKyU6ZJUeUnKdJwGx8uaro+K4QQfOR7vpA==
Expand Down

0 comments on commit f2a9147

Please sign in to comment.