Skip to content

Commit

Permalink
fix: handle document fragments
Browse files Browse the repository at this point in the history
closes #739
  • Loading branch information
atomiks committed Mar 24, 2020
1 parent c2ef6c3 commit 7c80e61
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/dom-utils.ts
Expand Up @@ -6,8 +6,8 @@ export function div(): HTMLDivElement {
return document.createElement('div');
}

export function isElement(value: unknown): value is Element {
return isType(value, 'Element');
export function isElement(value: unknown): value is Element | DocumentFragment {
return ['Element', 'Fragment'].some((type) => isType(value, type));
}

export function isNodeList(value: unknown): value is NodeList {
Expand Down
8 changes: 6 additions & 2 deletions src/types.ts
Expand Up @@ -4,7 +4,11 @@ export type BasePlacement = Popper.BasePlacement;

export type Placement = Popper.Placement;

export type Content = string | Element | ((ref: Element) => Element | string);
export type Content =
| string
| Element
| DocumentFragment
| ((ref: Element) => Element | string);

export type SingleTarget = Element;

Expand Down Expand Up @@ -44,7 +48,7 @@ export interface LifecycleHooks<TProps = Props> {
export interface RenderProps {
allowHTML: boolean;
animation: string | boolean;
arrow: boolean | string | SVGElement;
arrow: boolean | string | SVGElement | DocumentFragment;
content: Content;
inertia: boolean;
maxWidth: number | string;
Expand Down
11 changes: 11 additions & 0 deletions test/integration/__snapshots__/props.test.js.snap
@@ -1,5 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`content DocumentFragment is injected into the tippy node 1`] = `
<div
class="__NAMESPACE_PREFIX__-content"
data-state="hidden"
>
<button
class="__tippy"
/>
</div>
`;

exports[`content Element is injected into the tippy node 1`] = `
<div
class="__NAMESPACE_PREFIX__-content"
Expand Down
46 changes: 38 additions & 8 deletions test/integration/props.test.js
Expand Up @@ -146,6 +146,17 @@ describe('content', () => {
});
});

describe('DocumentFragment', () => {
it('is injected into the tippy node', () => {
const fragment = document.createDocumentFragment();
const node = h();
fragment.appendChild(node);
const instance = tippy(h(), {content: fragment});

expect(getChildren(instance.popper).content).toMatchSnapshot();
});
});

describe('Function', () => {
it('is injected into the tippy node', () => {
const instance = tippy(h(), {content: () => 'string'});
Expand Down Expand Up @@ -1316,16 +1327,35 @@ describe('arrow', () => {
});

describe('Element', () => {
const instance = tippy(h(), {
arrow: document.createElementNS('http://www.w3.org/2000/svg', 'svg'),
it('uses an svg', () => {
const instance = tippy(h(), {
arrow: document.createElementNS('http://www.w3.org/2000/svg', 'svg'),
});

expect(getChildren(instance.popper).arrow.className).toBe(
'__NAMESPACE_PREFIX__-svg-arrow'
);
expect(getChildren(instance.popper).arrow.querySelector('svg')).not.toBe(
null
);
});
});

expect(getChildren(instance.popper).arrow.className).toBe(
'__NAMESPACE_PREFIX__-svg-arrow'
);
expect(getChildren(instance.popper).arrow.querySelector('svg')).not.toBe(
null
);
describe('Fragment', () => {
it('uses an svg', () => {
const fragment = document.createDocumentFragment();
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');

fragment.appendChild(svg);

const instance = tippy(h(), {
arrow: fragment,
});

expect(getChildren(instance.popper).arrow.querySelector('svg')).not.toBe(
null
);
});
});

it('is updated correctly by .setProps()', () => {
Expand Down
13 changes: 9 additions & 4 deletions test/visual/tests.js
Expand Up @@ -40,12 +40,17 @@ window.state = {
const tests = window.state.tests;

tests.default = () => {
const content = document.createDocumentFragment();
const svgA = document.createElement('svg');
const svgB = document.createElement('svg');

content.appendChild(svgA);
content.appendChild(svgB);

const [instance] = tippy('#default .reference', {
content: 'Tippy',
content: 'hello',
arrow: content,
interactive: true,
aria: {
expanded: false,
},
trigger: 'click',
});

Expand Down

0 comments on commit 7c80e61

Please sign in to comment.