/
blockquote.js
211 lines (183 loc) · 5.79 KB
/
blockquote.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { props, define, hasNativeShadowDomSupport } from '@bolt/core/utils';
import { withLitHtml, html } from '@bolt/core/renderers/renderer-lit-html';
import { convertInitialTags } from '@bolt/core/decorators';
import classNames from 'classnames/bind';
import styles from './blockquote.scss';
import schema from '../blockquote.schema.yml';
import { AuthorImage, AuthorName, AuthorTitle } from './Author';
let cx = classNames.bind(styles);
@define
@convertInitialTags('blockquote') // The first matching tag will have its attributes converted to component props
class BoltBlockquote extends withLitHtml() {
static is = 'bolt-blockquote';
static props = {
size: props.string,
alignItems: props.string,
border: props.string,
indent: props.boolean,
fullBleed: props.boolean,
authorName: props.string,
authorTitle: props.string,
authorImage: props.string,
};
// https://github.com/WebReflection/document-register-element#upgrading-the-constructor-context
constructor(self) {
self = super(self);
self.useShadow = hasNativeShadowDomSupport;
self.schema = this.getModifiedSchema(schema);
return self;
}
rendered() {
super.rendered && super.rendered();
const self = this;
if (window.MutationObserver) {
// Re-generate slots + re-render when mutations are observed
const mutationCallback = function(mutationsList, observer) {
self.slots = self._checkSlots();
self.triggerUpdate();
};
// Create an observer instance linked to the callback function
self.observer = new MutationObserver(mutationCallback);
// Start observing the target node for configured mutations
self.observer.observe(this, {
attributes: false,
childList: true,
subtree: true,
});
}
}
disconnected() {
super.disconnected && super.disconnected();
// remove MutationObserver if supported + exists
if (window.MutationObserver && this.observer) {
this.observer.disconnect();
}
}
getModifiedSchema(schema) {
var modifiedSchema = schema;
// Remove "content" from schema, does not apply to web component.
for (let property in modifiedSchema.properties) {
if (property === 'content') {
delete modifiedSchema.properties[property];
}
}
const index = modifiedSchema.required.indexOf('content');
modifiedSchema.required.splice(index, 1);
return modifiedSchema;
}
getAlignItemsOption(prop) {
switch (prop) {
case 'right':
return 'end';
case 'center':
return 'center';
default:
// left => start
return 'start';
}
}
getBorderOption(prop) {
switch (prop) {
case 'none':
return 'borderless';
case 'horizontal':
return 'bordered-horizontal';
default:
// vertical => bordered-vertical
return 'bordered-vertical';
}
}
// automatically adds classes for the first and last slotted item (in the default slot) to help with tricky ::slotted selectors
addClassesToSlottedChildren() {
if (this.slots) {
if (this.slots.default) {
const defaultSlot = [];
this.slots.default.forEach(item => {
if (item.tagName) {
item.classList.remove('is-first-child');
item.classList.remove('is-last-child'); // clean up existing classes
defaultSlot.push(item);
}
});
if (defaultSlot[0]) {
defaultSlot[0].classList.add('is-first-child');
if (defaultSlot.length === 1) {
defaultSlot[0].classList.add('is-last-child');
}
}
if (defaultSlot[defaultSlot.length - 1]) {
defaultSlot[defaultSlot.length - 1].classList.add('is-last-child');
}
}
}
}
render() {
// validate the original prop data passed along -- returns back the validated data w/ added default values
const {
size,
alignItems,
border,
indent,
fullBleed,
authorName,
authorTitle,
authorImage,
} = this.validateProps(this.props);
const classes = cx('c-bolt-blockquote', {
[`c-bolt-blockquote--${size}`]: size,
[`c-bolt-blockquote--align-items-${this.getAlignItemsOption(
alignItems,
)}`]: this.getAlignItemsOption(alignItems),
[`c-bolt-blockquote--${this.getBorderOption(
border,
)}`]: this.getBorderOption(border),
[`c-bolt-blockquote--indented`]: indent,
[`c-bolt-blockquote--full`]: fullBleed,
});
let footerItems = [];
footerItems.push(AuthorImage(this), AuthorName(this), AuthorTitle(this));
this.addClassesToSlottedChildren();
return html`
${this.addStyles([styles])}
<blockquote class="${classes}" is="shadow-root">
${
this.slots.logo
? html`
<div class="${cx('c-bolt-blockquote__logo')}">
${this.slot('logo')}
</div>
`
: ''
}
<div class="${cx('c-bolt-blockquote__quote')}">
<bolt-text
tag="div"
font-size="${size}"
font-weight="semibold"
color="theme-headline"
>
${this.slot('default')}
</bolt-text>
</div>
${
footerItems.length > 0
? html`
<footer class="${cx('c-bolt-blockquote__footer')}">
${
footerItems.map(
footerItem => html`
<div class="${cx('c-bolt-blockquote__footer-item')}">
${footerItem}
</div>
`,
)
}
</footer>
`
: ''
}
</blockquote>
`;
}
}
export { BoltBlockquote };