Skip to content

Commit 2826575

Browse files
author
Daniel Morse
committed
feat: switch link and button to extend bolt-action, remove parts no longer needed
1 parent ad9cea1 commit 2826575

File tree

2 files changed

+8
-154
lines changed

2 files changed

+8
-154
lines changed

packages/components/bolt-button/src/button.standalone.js

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import {
22
props,
33
define,
4-
declarativeClickHandler,
5-
sanitizeBoltClasses,
6-
hasNativeShadowDomSupport,
7-
afterNextRender,
8-
watchForComponentMutations,
94
} from '@bolt/core/utils';
105
import {
11-
withLitHtml,
126
html,
137
render,
148
} from '@bolt/core/renderers/renderer-lit-html';
9+
import { BoltAction } from '@bolt/core/renderers/bolt-action';
1510

1611
import classNames from 'classnames/bind';
1712

@@ -21,7 +16,7 @@ import styles from './button.scss';
2116
let cx = classNames.bind(styles);
2217

2318
@define
24-
class BoltButton extends withLitHtml() {
19+
class BoltButton extends BoltAction {
2520
static is = 'bolt-button';
2621

2722
static props = {
@@ -37,86 +32,17 @@ class BoltButton extends withLitHtml() {
3732
disabled: props.boolean,
3833
target: props.string,
3934
url: props.string,
40-
4135
onClick: props.string, // Managed by base class
4236
onClickTarget: props.string, // Managed by base class
4337
};
4438

4539
// https://github.com/WebReflection/document-register-element#upgrading-the-constructor-context
4640
constructor(self) {
4741
self = super(self);
48-
self.useShadow = hasNativeShadowDomSupport;
42+
self.rootElementTags = ['button', 'a'];
4943
return self;
5044
}
5145

52-
connecting() {
53-
const root = this;
54-
55-
// If the initial <bolt-button> element contains a button or link, break apart the original HTML so we can retain any button or a tags but swap out the inner content with slots.
56-
57-
// Make sure the button component ONLY ever reuses any existing HTML ONCE. This, in part, helps to prevent rendering diff errors in Lit-HTML after booting up!
58-
if (this._wasInitiallyRendered === false) {
59-
this.childNodes.forEach((childElement, i) => {
60-
if (childElement.tagName === 'BUTTON' || childElement.tagName === 'A') {
61-
root.rootElement = document.createDocumentFragment();
62-
63-
// Take any existing buttons and links and move them to the root of the custom element
64-
while (childElement.firstChild) {
65-
root.appendChild(childElement.firstChild);
66-
}
67-
68-
if (childElement.className) {
69-
childElement.className = sanitizeBoltClasses(childElement);
70-
}
71-
72-
if (
73-
childElement.getAttribute('is') &&
74-
childElement.getAttribute('is') === 'shadow-root'
75-
) {
76-
childElement.removeAttribute('is');
77-
}
78-
79-
root.rootElement.appendChild(childElement);
80-
}
81-
});
82-
}
83-
84-
// When possible, use afterNextRender to defer non-critical work until after first paint.
85-
afterNextRender(this, function() {
86-
this.addEventListener('click', this.clickHandler);
87-
});
88-
}
89-
90-
rendered() {
91-
super.rendered(); // ensure any events emitted by the Bolt Base class fire as expected
92-
93-
// re-render if Shadow DOM is supported and enabled; temp workaround to dealing w/ components already rendered, but without slot support
94-
if (hasNativeShadowDomSupport && this.useShadow) {
95-
this.observer = watchForComponentMutations(this);
96-
97-
this.observer.observe(this, {
98-
attributes: false,
99-
childList: true,
100-
characterData: false,
101-
});
102-
}
103-
}
104-
105-
disconnecting() {
106-
this.removeEventListener('click', this.clickHandler);
107-
108-
if (hasNativeShadowDomSupport && this.useShadow) {
109-
if (this.observer) {
110-
this.observer.disconnect();
111-
}
112-
}
113-
}
114-
115-
// Attach external events declaratively
116-
clickHandler(event) {
117-
declarativeClickHandler(this);
118-
}
119-
12046
render() {
12147
const classes = cx('c-bolt-button', {
12248
'c-bolt-button--disabled': this.props.disabled,

packages/components/bolt-link/src/link.js

Lines changed: 5 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import {
22
props,
33
define,
4-
declarativeClickHandler,
5-
sanitizeBoltClasses,
6-
hasNativeShadowDomSupport,
7-
afterNextRender,
8-
watchForComponentMutations,
94
} from '@bolt/core/utils';
10-
import classNames from 'classnames/bind';
115
import {
12-
withLitHtml,
136
html,
147
render,
158
} from '@bolt/core/renderers/renderer-lit-html';
9+
import { BoltAction } from '@bolt/core/renderers/bolt-action';
10+
11+
import classNames from 'classnames/bind';
1612

1713
import styles from './link.scss';
1814
import schema from '../link.schema.yml';
1915

2016
let cx = classNames.bind(styles);
2117

2218
@define
23-
class BoltLink extends withLitHtml() {
19+
class BoltLink extends BoltAction {
2420
static is = 'bolt-link';
2521

2622
static props = {
@@ -34,78 +30,10 @@ class BoltLink extends withLitHtml() {
3430
// https://github.com/WebReflection/document-register-element#upgrading-the-constructor-context
3531
constructor(self) {
3632
self = super(self);
37-
self.useShadow = hasNativeShadowDomSupport;
33+
self.rootElementTags = ['a'];
3834
return self;
3935
}
4036

41-
connecting() {
42-
const root = this;
43-
44-
// If the initial <bolt-link> element contains a link, break apart the original HTML so we can retain the a tag but swap out the inner content with slots.
45-
46-
// Make sure the button component ONLY ever reuses any existing HTML ONCE. This, in part, helps to prevent rendering diff errors in HyperHTML after booting up!
47-
if (this._wasInitiallyRendered === false) {
48-
this.childNodes.forEach((childElement, i) => {
49-
if (childElement.tagName === 'A') {
50-
root.rootElement = document.createDocumentFragment();
51-
52-
// Take any existing elements and move them to the root of the custom element
53-
while (childElement.firstChild) {
54-
root.appendChild(childElement.firstChild);
55-
}
56-
57-
if (childElement.className) {
58-
childElement.className = sanitizeBoltClasses(childElement);
59-
}
60-
61-
if (
62-
childElement.getAttribute('is') &&
63-
childElement.getAttribute('is') === 'shadow-root'
64-
) {
65-
childElement.removeAttribute('is');
66-
}
67-
68-
root.rootElement.appendChild(childElement);
69-
}
70-
});
71-
}
72-
73-
// When possible, use afterNextRender to defer non-critical work until after first paint.
74-
afterNextRender(this, function() {
75-
this.addEventListener('click', this.clickHandler);
76-
});
77-
}
78-
79-
rendered() {
80-
super.rendered(); // ensure any events emitted by the Bolt Base class fire as expected
81-
82-
// re-render if Shadow DOM is supported and enabled; temp workaround to dealing w/ components already rendered, but without slot support
83-
if (hasNativeShadowDomSupport && this.useShadow) {
84-
this.observer = watchForComponentMutations(this);
85-
86-
this.observer.observe(this, {
87-
attributes: false,
88-
childList: true,
89-
characterData: false,
90-
});
91-
}
92-
}
93-
94-
disconnecting() {
95-
this.removeEventListener('click', this.clickHandler);
96-
97-
if (hasNativeShadowDomSupport && this.useShadow) {
98-
if (this.observer) {
99-
this.observer.disconnect();
100-
}
101-
}
102-
}
103-
104-
// Attach external events declaratively
105-
clickHandler(event) {
106-
declarativeClickHandler(this);
107-
}
108-
10937
render() {
11038
// validate the original prop data passed along -- returns back the validated data w/ added default values
11139
const { url, target, isHeadline } = this.validateProps(schema, this.props);

0 commit comments

Comments
 (0)