Skip to content

Commit

Permalink
feat(addon/components/paper-button): migrates off proxiable mixin.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhartstonge committed Nov 12, 2024
1 parent c8f160e commit 1ca0f88
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
3 changes: 1 addition & 2 deletions addon/components/paper-button.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
target={{@target}}
title={{@title}}
type={{this.type}}
{{did-insert this.registerListeners}}
{{will-destroy this.unregisterListeners}}
{{did-insert this.didInsertNode}}
{{on 'click' this.handleClick}}
...attributes
>
Expand Down
89 changes: 89 additions & 0 deletions addon/components/paper-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,67 @@
* @module ember-paper
*/
import Focusable from './-focusable';
import { tracked } from '@glimmer/tracking';

Check failure on line 5 in addon/components/paper-button.js

View workflow job for this annotation

GitHub Actions / Tests

'tracked' is defined but never used
import { action } from '@ember/object';
import { assert } from '@ember/debug';

/**
* @class PaperButton
* @extends Focusable
*/
export default class PaperButton extends Focusable {
/**
* Reference to the component's DOM element
* @type {HTMLElement}
*/
element;
/**
* The parent this component is bound to.
* @type {Boolean}
*/
parent;
/**
* Marks whether the component should register itself to the supplied parent
* @type {Boolean}
*/
shouldRegister;

constructor(owner, args) {
super(owner, args);

this.shouldRegister = this.args.shouldRegister || false;
if (this.shouldRegister) {
assert(
'A parent component should be supplied to <PaperButton> when shouldRegister=true',
this.args.parent
);
this.parent = this.args.parent;
}
}

@action didInsertNode(element) {
this.element = element;
this.registerListeners(element);

if (this.shouldRegister) {
this.parent.registerChild(this);
}
}

@action didUpdateNode() {
// noop
}

willDestroy() {
super.willDestroy(...arguments);

if (this.shouldRegister) {
this.parent.deregisterChild(this);
}

this.deregisterListeners(this.element);
}

get tag() {
if (this.args.href) {
return 'a';
Expand Down Expand Up @@ -41,4 +95,39 @@ export default class PaperButton extends Focusable {

return this.args.bubbles;
}

// Proxiable Handlers

@action handleMouseDown(e) {
super.handleMouseDown(e);

let parentComponent = this.parentComponent;
if (parentComponent) {
parentComponent.mouseActive = true;
setTimeout(() => {
if (parentComponent.isDestroyed) {
return;
}
parentComponent.mouseActive = false;
}, 100);
}
}

@action handleFocusIn(e) {
super.handleFocusIn(e);

let parentComponent = this.parent;
if (parentComponent && !parentComponent.mouseActive) {
parentComponent.focused = true;
}
}

@action focusOut(e) {
super.focusOut(e);

let parentComponent = this.parent;
if (parentComponent) {
parentComponent.focused = false;
}
}
}

0 comments on commit 1ca0f88

Please sign in to comment.