Skip to content

Commit 4930aa5

Browse files
authored
fix(gatsby): Refactor overlay utils (#31005)
1 parent 7838b18 commit 4930aa5

File tree

8 files changed

+58
-116
lines changed

8 files changed

+58
-116
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Converts a string of CSS into object syntax
3+
* @param strings
4+
* @param keys
5+
* @returns {Object} CSS in object syntax
6+
* @example
7+
* const output = css`
8+
* html {
9+
* color: rebeccapurple;
10+
* }
11+
* `
12+
*/
13+
export function css(strings, ...keys) {
14+
const lastIndex = strings.length - 1
15+
return (
16+
strings.slice(0, lastIndex).reduce((p, s, i) => p + s + keys[i], ``) +
17+
strings[lastIndex]
18+
)
19+
}

packages/gatsby/cache-dir/fast-refresh-overlay/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react"
22
import { ErrorBoundary } from "./components/error-boundary"
3-
import { ShadowPortal } from "./components/portal"
3+
import { ShadowPortal } from "../shadow-portal"
44
import { Style } from "./style"
55
import { BuildError } from "./components/build-error"
66
import { RuntimeErrors } from "./components/runtime-errors"
@@ -102,7 +102,7 @@ function DevOverlay({ children }) {
102102
<React.Fragment>
103103
<ErrorBoundary hasErrors={hasErrors}>{children ?? null}</ErrorBoundary>
104104
{hasErrors ? (
105-
<ShadowPortal>
105+
<ShadowPortal identifier="gatsby-fast-refresh">
106106
<Style />
107107
<ErrorComponent />
108108
</ShadowPortal>

packages/gatsby/cache-dir/fast-refresh-overlay/style.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import * as React from "react"
2-
3-
function css(strings, ...keys) {
4-
const lastIndex = strings.length - 1
5-
return (
6-
strings.slice(0, lastIndex).reduce((p, s, i) => p + s + keys[i], ``) +
7-
strings[lastIndex]
8-
)
9-
}
2+
import { css } from "../css-to-object"
103

114
export const Style = () => (
125
<style
@@ -46,8 +39,8 @@ export const Style = () => (
4639
--codeFrame-color: #414141;
4740
--codeFrame-button-bg: white;
4841
--radii: 5px;
49-
--z-index-backdrop: 8000;
50-
--z-index-overlay: 9000;
42+
--z-index-backdrop: 9000;
43+
--z-index-overlay: 10000;
5144
--space: 1.5em;
5245
--space-sm: 1em;
5346
--space-lg: 2.5em;
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
1-
import React from "react"
2-
1+
import * as React from "react"
32
import emitter from "../emitter"
43
import { Indicator } from "./indicator"
54

6-
// no hooks because we support react versions without hooks support
7-
export class LoadingIndicatorEventHandler extends React.Component {
8-
state = { visible: false }
9-
10-
show = () => {
11-
this.setState({ visible: true })
12-
}
13-
14-
hide = () => {
15-
this.setState({ visible: false })
16-
}
5+
export function LoadingIndicatorEventHandler() {
6+
const [visible, setVisible] = React.useState(false)
177

18-
componentDidMount() {
19-
emitter.on(`onDelayedLoadPageResources`, this.show)
20-
emitter.on(`onRouteUpdate`, this.hide)
21-
}
8+
React.useEffect(() => {
9+
emitter.on(`onDelayedLoadPageResources`, () => setVisible(true))
10+
emitter.on(`onRouteUpdate`, () => setVisible(false))
2211

23-
componentWillUnmount() {
24-
emitter.off(`onDelayedLoadPageResources`, this.show)
25-
emitter.off(`onRouteUpdate`, this.hide)
26-
}
12+
return () => {
13+
emitter.off(`onDelayedLoadPageResources`, () => setVisible(true))
14+
emitter.off(`onRouteUpdate`, () => setVisible(false))
15+
}
16+
}, [])
2717

28-
render() {
29-
return <Indicator visible={this.state.visible} />
30-
}
18+
return <Indicator visible={visible} />
3119
}

packages/gatsby/cache-dir/loading-indicator/indicator.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from "react"
2-
import Portal from "./portal"
3-
import Style from "./style"
1+
import * as React from "react"
2+
import { ShadowPortal } from "../shadow-portal"
3+
import { Style } from "./style"
44
import { isLoadingIndicatorEnabled } from "$virtual/loading-indicator"
55
import { debugLog } from "../debug-log"
66

@@ -38,7 +38,7 @@ export function Indicator({ visible = true }) {
3838
}
3939

4040
return (
41-
<Portal>
41+
<ShadowPortal identifier="gatsby-qod">
4242
<Style />
4343
<div
4444
data-gatsby-loading-indicator="root"
@@ -60,6 +60,6 @@ export function Indicator({ visible = true }) {
6060
{visible ? `Preparing requested page` : ``}
6161
</div>
6262
</div>
63-
</Portal>
63+
</ShadowPortal>
6464
)
6565
}

packages/gatsby/cache-dir/loading-indicator/portal.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

packages/gatsby/cache-dir/loading-indicator/style.js

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
import React from "react"
2+
import { css } from "../css-to-object"
23

3-
function css(strings, ...keys) {
4-
const lastIndex = strings.length - 1
5-
return (
6-
strings.slice(0, lastIndex).reduce((p, s, i) => p + s + keys[i], ``) +
7-
strings[lastIndex]
8-
)
9-
}
10-
11-
const Style = () => (
4+
export const Style = () => (
125
<style
136
dangerouslySetInnerHTML={{
147
__html: css`
158
:host {
16-
--purple-60: #663399;
17-
--gatsby: var(--purple-60);
18-
--purple-40: #b17acc;
19-
--purple-20: #f1defa;
20-
--dimmedWhite: rgba(255, 255, 255, 0.8);
21-
--white: #ffffff;
22-
--black: #000000;
23-
--grey-90: #232129;
9+
--spinnerColor: #663399;
10+
--borderLeft: #b17acc;
11+
--background: #ffffff;
12+
--color: #232129;
2413
--radii: 4px;
25-
--z-index-indicator: 9000;
14+
--z-index-indicator: 10000;
2615
--shadow: 0px 2px 4px rgba(46, 41, 51, 0.08),
2716
0px 4px 8px rgba(71, 63, 79, 0.16);
2817
}
@@ -31,15 +20,15 @@ const Style = () => (
3120
font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
3221
Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
3322
"Segoe UI Symbol" !important;
34-
background: var(--white);
35-
color: var(--grey-90);
23+
background: var(--background);
24+
color: var(--color);
3625
position: fixed;
3726
bottom: 1.5em;
3827
left: 1.5em;
3928
box-shadow: var(--shadow);
4029
border-radius: var(--radii);
4130
z-index: var(--z-index-indicator);
42-
border-left: 0.25em solid var(--purple-40);
31+
border-left: 0.25em solid var(--borderLeft);
4332
display: flex;
4433
align-items: center;
4534
justify-content: space-between;
@@ -67,7 +56,7 @@ const Style = () => (
6756
animation: spin 1s linear infinite;
6857
height: 18px;
6958
width: 18px;
70-
color: var(--gatsby);
59+
color: var(--spinnerColor);
7160
}
7261
7362
[data-gatsby-loading-indicator="text"] {
@@ -98,17 +87,13 @@ const Style = () => (
9887
}
9988
10089
@media (prefers-color-scheme: dark) {
101-
[data-gatsby-loading-indicator="root"] {
102-
background: var(--grey-90);
103-
color: var(--white);
104-
}
105-
[data-gatsby-loading-indicator="spinner"] {
106-
color: var(--purple-20);
90+
:host {
91+
--spinnerColor: #f1defa;
92+
--background: #232129;
93+
--color: #ffffff;
10794
}
10895
}
10996
`,
11097
}}
11198
/>
11299
)
113-
114-
export default Style

packages/gatsby/cache-dir/fast-refresh-overlay/components/portal.js renamed to packages/gatsby/cache-dir/shadow-portal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import * as React from "react"
22
import { createPortal } from "react-dom"
33

4-
export const ShadowPortal = function Portal({ children }) {
4+
export const ShadowPortal = ({ children, identifier }) => {
55
const mountNode = React.useRef(null)
66
const portalNode = React.useRef(null)
77
const shadowNode = React.useRef(null)
88
const [, forceUpdate] = React.useState()
99

1010
React.useLayoutEffect(() => {
1111
const ownerDocument = mountNode.current.ownerDocument
12-
portalNode.current = ownerDocument.createElement(`gatsby-fast-refresh`)
12+
portalNode.current = ownerDocument.createElement(identifier)
1313
shadowNode.current = portalNode.current.attachShadow({ mode: `open` })
1414
ownerDocument.body.appendChild(portalNode.current)
1515
forceUpdate({})

0 commit comments

Comments
 (0)