Skip to content

Commit 04a0107

Browse files
committed
fix(code-block)!: remove BaseCodeBlock
Closes #2613
1 parent 24d43bd commit 04a0107

File tree

6 files changed

+37
-50
lines changed

6 files changed

+37
-50
lines changed

.changeset/remove-basecodeblock.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@patternfly/elements": major
3+
---
4+
`<pf-code-block>`: Removed `BaseCodeBlock` class. Reimplement (recommended) or extend `PfCodeBlock`.

elements/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"./pf-card/pf-card.js": "./pf-card/pf-card.js",
2828
"./pf-clipboard-copy/BaseClipboardCopy.js": "./pf-clipboard-copy/BaseClipboardCopy.js",
2929
"./pf-clipboard-copy/pf-clipboard-copy.js": "./pf-clipboard-copy/pf-clipboard-copy.js",
30-
"./pf-code-block/BaseCodeBlock.js": "./pf-code-block/BaseCodeBlock.js",
3130
"./pf-code-block/pf-code-block.js": "./pf-code-block/pf-code-block.js",
3231
"./pf-icon/BaseIcon.js": "./pf-icon/BaseIcon.js",
3332
"./pf-icon/pf-icon.js": "./pf-icon/pf-icon.js",

elements/pf-code-block/BaseCodeBlock.css

-7
This file was deleted.

elements/pf-code-block/BaseCodeBlock.ts

-28
This file was deleted.

elements/pf-code-block/pf-code-block.css

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
:host {
2+
display: block;
23
background-color: var(--pf-c-code-block--BackgroundColor, #f0f0f0);
34
font-size: var(--pf-c-code-block__pre--FontSize, 0.875rem);
45
font-family: var(--pf-c-code-block__pre--FontFamily, "Liberation Mono", consolas, "SFMono-Regular", menlo, monaco, "Courier New", monospace);
56
}
67

8+
[hidden] {
9+
display: none !important;
10+
}
11+
712
#container {
13+
margin: 0;
814
padding-top: var(--pf-c-code-block__content--PaddingTop, 1rem);
915
padding-right: var(--pf-c-code-block__content--PaddingRight, 1rem);
1016
padding-bottom: var(--pf-c-code-block__content--PaddingBottom, 1rem);
@@ -15,10 +21,6 @@
1521
border-bottom: var(--pf-c-code-block__header--BorderBottomWidth, 1px) solid var(--pf-c-code-block__header--BorderBottomColor, #d2d2d2);
1622
}
1723

18-
#container {
19-
margin: 0;
20-
}
21-
2224
pre {
2325
margin: 0;
2426
}

elements/pf-code-block/pf-code-block.ts

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { html } from 'lit';
1+
import { LitElement, html } from 'lit';
22
import { customElement } from 'lit/decorators/custom-element.js';
33
import { property } from 'lit/decorators/property.js';
44
import { classMap } from 'lit/directives/class-map.js';
5-
import { BaseCodeBlock } from './BaseCodeBlock.js';
65
import styles from './pf-code-block.css';
76

87
/**
@@ -28,20 +27,34 @@ import styles from './pf-code-block.css';
2827
* @cssprop {<string>} --pf-c-code-block__pre--FontFamily {@default `"Liberation Mono", consolas, "SFMono-Regular", menlo, monaco, "Courier New", monospace`}
2928
*/
3029

30+
function dedent(str: string): string {
31+
const stripped = str.replace(/^\n/, '');
32+
const match = stripped.match(/^\s+/);
33+
return match ? stripped.replace(new RegExp(`^${match[0]}`, 'gm'), '') : str;
34+
}
35+
3136
@customElement('pf-code-block')
32-
export class PfCodeBlock extends BaseCodeBlock {
33-
static readonly styles = [...BaseCodeBlock.styles, styles];
37+
export class PfCodeBlock extends LitElement {
38+
static readonly styles = [styles];
3439

40+
/** Flag for whether the code block is expanded */
3541
@property({ type: Boolean, reflect: true }) expanded = false;
3642

37-
#toggle() {
38-
this.expanded = !this.expanded;
39-
}
40-
4143
get #expandedContent(): string {
4244
return this.querySelector('script[data-expand]')?.textContent ?? '';
4345
}
4446

47+
get #content() {
48+
const script = this.querySelector<HTMLScriptElement>('script[type]');
49+
if (
50+
script?.type !== 'text/javascript-sample' &&
51+
!!script?.type.match(/(j(ava)?|ecma|live)script/)) {
52+
return '';
53+
} else {
54+
return dedent(script?.textContent ?? '');
55+
}
56+
}
57+
4558
override render() {
4659
const { expanded } = this;
4760
return html`
@@ -51,8 +64,8 @@ export class PfCodeBlock extends BaseCodeBlock {
5164
</div>
5265
</div>
5366
<div id="container" class="${classMap({ expanded })}">
54-
<pre><code id="content">${this.content}</code><code id="code-block-expand"
55-
?hidden="${!expanded}">${this.#expandedContent}</code></pre>
67+
<pre><code id="content">${this.#content}</code><code id="code-block-expand"
68+
?hidden="${!expanded}">${this.#expandedContent}</code></pre>
5669
<button ?hidden="${!this.#expandedContent}"
5770
@click=${this.#toggle}
5871
aria-expanded=${this.expanded}
@@ -63,6 +76,10 @@ export class PfCodeBlock extends BaseCodeBlock {
6376
</div>
6477
`;
6578
}
79+
80+
#toggle() {
81+
this.expanded = !this.expanded;
82+
}
6683
}
6784

6885
declare global {

0 commit comments

Comments
 (0)