Skip to content

Commit

Permalink
fix(core): teleported elements theme class
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode committed Nov 15, 2023
1 parent b187a6a commit be8a860
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 6 additions & 2 deletions packages/carta-md/src/lib/internal/carta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ export class Carta {

// Bind elements
this.elementsToBind.forEach((it) => {
it.callback = this.input?.$bindToCaret(it.elem, it.portal).destroy;
it.callback = this.input?.$bindToCaret(it.elem, {
portal: it.portal,
editorElement: this.element
}).destroy;
});
}

Expand Down Expand Up @@ -364,7 +367,8 @@ export class Carta {
public bindToCaret(element: HTMLElement, portal = document.querySelector('body') as HTMLElement) {
let callback: (() => void) | undefined;

if (this.input) callback = this.input.$bindToCaret(element, portal).destroy;
if (this.input)
callback = this.input.$bindToCaret(element, { portal, editorElement: this.element }).destroy;

// Bind the element later, when the input is ready
this.elementsToBind.push({ elem: element, portal, callback });
Expand Down
11 changes: 7 additions & 4 deletions packages/carta-md/src/lib/internal/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,14 @@ export class CartaInput {
* @param elem The element to position.
* @param portal The portal to append the element to. Defaults to `document.body`.
*/
public $bindToCaret(elem: HTMLElement, portal: HTMLElement) {
public $bindToCaret(
elem: HTMLElement,
data: { portal: HTMLElement; editorElement?: HTMLElement }
) {
// Move the element to body
portal.appendChild(elem);
data.portal.appendChild(elem);
// Add theme class as the the teleported element is not a child of the container
const themeClass = Array.from(this.container.classList).find((c) =>
const themeClass = Array.from(data.editorElement?.classList ?? []).find((c) =>
c.startsWith('carta-theme__')
);
elem.classList.add(themeClass ?? 'carta-theme__default');
Expand Down Expand Up @@ -499,7 +502,7 @@ export class CartaInput {
return {
destroy: () => {
try {
portal.removeChild(elem);
data.portal.removeChild(elem);
} catch (e: unknown) {
// Ignore
}
Expand Down

0 comments on commit be8a860

Please sign in to comment.