Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/selfish-apes-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hashicorp/design-system-components": patch
---

`CodeBlock` - Fixed issues with line numbers when line wrapping is present, and line highlighting when hidden from view initially such as inside a tabs component
4 changes: 2 additions & 2 deletions packages/components/src/components/hds/code-block/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SPDX-License-Identifier: MPL-2.0
}}

<div class={{this.classNames}} ...attributes>
<div class={{this.classNames}} ...attributes {{this._setUpObserver}}>
<div class="hds-code-block__header">
{{~yield (hash Title=(component "hds/code-block/title"))~}}
{{~yield (hash Description=(component "hds/code-block/description"))~}}
Expand All @@ -17,7 +17,7 @@
data-start={{@lineNumberStart}}
id={{this._preCodeId}}
tabindex="0"
><code {{did-insert this.setPrismCode}} {{did-update this.setPrismCode this.code @language}}>
><code {{this._setUpCodeBlockCode}}>
{{~this._prismCode~}}
</code></pre>

Expand Down
56 changes: 42 additions & 14 deletions packages/components/src/components/hds/code-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { assert } from '@ember/debug';
import { next, schedule } from '@ember/runloop';
import { htmlSafe } from '@ember/template';
import { guidFor } from '@ember/object/internals';
import { modifier } from 'ember-modifier';

import Prism from 'prismjs';

Expand Down Expand Up @@ -75,6 +76,31 @@ export default class HdsCodeBlock extends Component<HdsCodeBlockSignature> {

// Generates a unique ID for the code content
private _preCodeId = 'pre-code-' + guidFor(this);
private _preCodeElement!: HTMLPreElement;
private _observer!: IntersectionObserver;

// If a code block is hidden from view, and made visible after load, the Prism code needs to be re-run
private _setUpObserver = modifier((element: HTMLElement) => {
this._preCodeElement = element.querySelector('pre') as HTMLPreElement;
const codeBlock = element.querySelector('code') as HTMLElement;
this._observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
this.setPrismCode(codeBlock);
}
});
});
this._observer.observe(element);

return () => {
this._observer.disconnect();
};
});

private _setUpCodeBlockCode = modifier((element: HTMLElement) => {
this.setPrismCode(element);
return () => {};
});

// code text content for the CodeBlock
get code(): string {
Expand Down Expand Up @@ -152,24 +178,26 @@ export default class HdsCodeBlock extends Component<HdsCodeBlockSignature> {
element,
});

// Get the actual height & the content height of the preCodeElement
// eslint-disable-next-line ember/no-runloop
schedule('afterRender', (): void => {
const preCodeElement = document.getElementById(this._preCodeId);
this._codeContentHeight = preCodeElement?.scrollHeight ?? 0;
this._codeContainerHeight = preCodeElement?.clientHeight ?? 0;
});

// Force prism-line-highlight plugin initialization
// Context: https://github.com/hashicorp/design-system/pull/1749#discussion_r1374288785
if (this.args.highlightLines) {
// we need to delay re-evaluating the context for prism-line-highlight for as much as possible, and `afterRender` is the 'latest' we can use in the component lifecycle
// eslint-disable-next-line ember/no-runloop
schedule('afterRender', (): void => {
// Get the actual height & the content height of the preCodeElement
this._codeContentHeight = this._preCodeElement?.scrollHeight ?? 0;
this._codeContainerHeight = this._preCodeElement?.clientHeight ?? 0;

// we need to re-trigger the line numbers generation as late as possible to account for any line wrapping styles that are applied
if (this.args.hasLineWrapping && Prism?.plugins?.['lineNumbers']) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
Prism.plugins['lineNumbers'].resize(this._preCodeElement);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use the same logic as the line highlight and piggy back on triggering a resize event because the Prism.js line number plugin does a check to see if the viewport width actually changes on a resize event. It aborts any updates if the viewport is unchanged.

}

// Force prism-line-highlight plugin initialization
// Context: https://github.com/hashicorp/design-system/pull/1749#discussion_r1374288785
if (this.args.highlightLines) {
// we need to delay re-evaluating the context for prism-line-highlight for as much as possible, and `afterRender` is the 'latest' we can use in the component lifecycle
// we piggy-back on the plugin's `resize` event listener to trigger a new call of the `highlightLines` function: https://github.com/PrismJS/prism/blob/master/plugins/line-highlight/prism-line-highlight.js#L337
if (window) window.dispatchEvent(new Event('resize'));
});
}
}
});
});
}
}
Expand Down