Skip to content

Commit cd4f27f

Browse files
feat(addon/components/paper-button): migrates off proxiable mixin.
1 parent a80286f commit cd4f27f

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

addon/components/paper-button.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
target={{@target}}
2020
title={{@title}}
2121
type={{this.type}}
22-
{{did-insert this.registerListeners}}
23-
{{will-destroy this.unregisterListeners}}
22+
{{did-insert this.didInsertNode}}
23+
{{will-destroy this.willDestroyNode}}
2424
{{on 'click' this.handleClick}}
2525
...attributes
2626
>

addon/components/paper-button.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,86 @@
22
* @module ember-paper
33
*/
44
import Focusable from './-focusable';
5+
import { tracked } from '@glimmer/tracking';
56
import { action } from '@ember/object';
7+
import { assert } from '@ember/debug';
68

79
/**
810
* @class PaperButton
911
* @extends Focusable
1012
*/
1113
export default class PaperButton extends Focusable {
14+
/**
15+
* Reference to the component's DOM element
16+
* @type {HTMLElement}
17+
*/
18+
@tracked element;
19+
/**
20+
* The parent this component is bound to.
21+
* @type {Boolean}
22+
*/
23+
parent;
24+
/**
25+
* Marks whether the component should register itself to the supplied parent
26+
* @type {Boolean}
27+
*/
28+
shouldRegister;
29+
/**
30+
* Marks whether the component should skip being proxied.
31+
* @type {Boolean}
32+
*/
33+
skipProxy;
34+
35+
constructor(owner, args) {
36+
super(owner, args);
37+
38+
this.shouldRegister = this.args.shouldRegister || false;
39+
this.skipProxy = this.args.skipProxy || false;
40+
if (this.shouldRegister) {
41+
assert(
42+
'A parent component should be supplied to <PaperButton> when shouldRegister=true',
43+
this.args.parentComponent
44+
);
45+
this.parent = this.args.parentComponent;
46+
}
47+
}
48+
49+
/**
50+
* Performs any required DOM setup.
51+
* @param element
52+
*/
53+
@action didInsertNode(element) {
54+
this.element = element;
55+
this.registerListeners(element);
56+
57+
if (this.shouldRegister) {
58+
this.args.parentComponent.registerChild(this);
59+
}
60+
}
61+
62+
/**
63+
* Performs DOM updates based on tracked args.
64+
*/
65+
@action didUpdateNode() {
66+
// noop
67+
}
68+
69+
/**
70+
* Performs any required DOM teardown.
71+
* @param element
72+
*/
73+
@action willDestroyNode(element) {
74+
this.unregisterListeners(element);
75+
}
76+
77+
willDestroy() {
78+
super.willDestroy(...arguments);
79+
80+
if (this.shouldRegister) {
81+
this.args.parentComponent.unregisterChild(this);
82+
}
83+
}
84+
1285
get tag() {
1386
if (this.args.href) {
1487
return 'a';
@@ -41,4 +114,39 @@ export default class PaperButton extends Focusable {
41114

42115
return this.args.bubbles;
43116
}
117+
118+
// Proxiable Handlers
119+
120+
@action handleMouseDown(e) {
121+
super.handleMouseDown(e);
122+
123+
let parentComponent = this.parentComponent;
124+
if (parentComponent) {
125+
parentComponent.mouseActive = true;
126+
setTimeout(() => {
127+
if (parentComponent.isDestroyed) {
128+
return;
129+
}
130+
parentComponent.mouseActive = false;
131+
}, 100);
132+
}
133+
}
134+
135+
@action handleFocusIn(e) {
136+
super.handleFocusIn(e);
137+
138+
let parentComponent = this.parent;
139+
if (parentComponent && !parentComponent.mouseActive) {
140+
parentComponent.focused = true;
141+
}
142+
}
143+
144+
@action focusOut(e) {
145+
super.focusOut(e);
146+
147+
let parentComponent = this.parent;
148+
if (parentComponent) {
149+
parentComponent.focused = false;
150+
}
151+
}
44152
}

0 commit comments

Comments
 (0)