Skip to content

Commit

Permalink
Merge branch 'main' into as-prop
Browse files Browse the repository at this point in the history
  • Loading branch information
colebemis authored Apr 8, 2021
2 parents 5007661 + 4a5f45a commit fbe0e25
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
18 changes: 9 additions & 9 deletions src/Portal/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ const portalRootRegistry: {[key: string]: Element} = {}
* @param name The name of the container, to be used with the `containerName` prop on the Portal Component.
* If name is not specified, registers the default portal root.
*/
export function registerPortalRoot(root: Element | undefined, name?: string): void {
if (root instanceof Element) {
portalRootRegistry[name ?? DEFAULT_PORTAL_CONTAINER_NAME] = root
} else {
delete portalRootRegistry[name ?? DEFAULT_PORTAL_CONTAINER_NAME]
}
export function registerPortalRoot(root: Element, name = DEFAULT_PORTAL_CONTAINER_NAME): void {
portalRootRegistry[name] = root
}

// Ensures that a default portal root exists and is registered. If a DOM element exists
// with id __primerPortalRoot__, allow that element to serve as the default portl root.
// with id __primerPortalRoot__, allow that element to serve as the default portal root.
// Otherwise, create that element and attach it to the end of document.body.
function ensureDefaultPortal() {
if (!(DEFAULT_PORTAL_CONTAINER_NAME in portalRootRegistry)) {
if (
!(DEFAULT_PORTAL_CONTAINER_NAME in portalRootRegistry) ||
!document.body.contains(portalRootRegistry[DEFAULT_PORTAL_CONTAINER_NAME])
) {
let defaultPortalContainer = document.getElementById(PRIMER_PORTAL_ROOT_ID)
if (!(defaultPortalContainer instanceof Element)) {
defaultPortalContainer = document.createElement('div')
Expand All @@ -36,7 +35,8 @@ function ensureDefaultPortal() {
document.body.appendChild(defaultPortalContainer)
}
}
portalRootRegistry[DEFAULT_PORTAL_CONTAINER_NAME] = defaultPortalContainer

registerPortalRoot(defaultPortalContainer)
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/__tests__/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import React from 'react'
import BaseStyles from '../BaseStyles'

describe('Portal', () => {
afterEach(() => {
// since the registry is global, reset after each test
registerPortalRoot(undefined)
})
it('renders a default portal into document.body (no BaseStyles present)', () => {
const {baseElement} = render(<Portal>123test123</Portal>)
const generatedRoot = baseElement.querySelector('#__primerPortalRoot__')
Expand Down
4 changes: 0 additions & 4 deletions src/stories/Overlay.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ import React, {useState, useRef} from 'react'
import {Meta} from '@storybook/react'
import styled from 'styled-components'

import {registerPortalRoot} from '../Portal'
import {BaseStyles, Overlay, Button, Text, ButtonDanger, ThemeProvider, Position, Flex} from '..'

export default {
title: 'Internal components/Overlay',
component: Overlay,
decorators: [
Story => {
// Since portal roots are registered globally, we need this line so that each storybook
// story works in isolation.
registerPortalRoot(undefined)
return (
<ThemeProvider>
<BaseStyles>
Expand Down
3 changes: 0 additions & 3 deletions src/stories/Portal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export default {
component: Portal,
decorators: [
Story => {
// Since portal roots are registered globally, we need this line so that each storybook
// story works in isolation.
registerPortalRoot(undefined)
return (
<ThemeProvider>
<BaseStyles>
Expand Down

0 comments on commit fbe0e25

Please sign in to comment.