From dcbba908418047ea3d436b10355c5291beca7401 Mon Sep 17 00:00:00 2001 From: Gil Barbara Date: Fri, 27 Oct 2023 19:06:28 -0300 Subject: [PATCH] Minor fixes --- src/components/Beacon.tsx | 29 ++++++++++++++++------------- src/components/Overlay.tsx | 4 ++-- src/components/index.tsx | 5 ++--- src/global.d.ts | 8 ++++++++ src/modules/dom.ts | 2 +- src/modules/helpers.ts | 2 +- src/modules/scope.ts | 4 ++-- test/modules/scope.spec.tsx | 1 - 8 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 src/global.d.ts diff --git a/src/components/Beacon.tsx b/src/components/Beacon.tsx index e1f120ed..976cd153 100644 --- a/src/components/Beacon.tsx +++ b/src/components/Beacon.tsx @@ -10,10 +10,20 @@ export default class JoyrideBeacon extends React.Component { constructor(props: BeaconProps) { super(props); - if (!props.beaconComponent) { - const head = document.head || document.getElementsByTagName('head')[0]; - const style = document.createElement('style'); - const css = ` + if (props.beaconComponent) { + return; + } + + const head = document.head || document.getElementsByTagName('head')[0]; + const style = document.createElement('style'); + + style.id = 'joyride-beacon-animation'; + + if (props.nonce) { + style.setAttribute('nonce', props.nonce); + } + + const css = ` @keyframes joyride-beacon-inner { 20% { opacity: 0.9; @@ -41,16 +51,9 @@ export default class JoyrideBeacon extends React.Component { } `; - style.id = 'joyride-beacon-animation'; - - if (props.nonce) { - style.setAttribute('nonce', props.nonce); - } + style.appendChild(document.createTextNode(css)); - style.appendChild(document.createTextNode(css)); - - head.appendChild(style); - } + head.appendChild(style); } componentDidMount() { diff --git a/src/components/Overlay.tsx b/src/components/Overlay.tsx index c470ab91..41fcf610 100644 --- a/src/components/Overlay.tsx +++ b/src/components/Overlay.tsx @@ -43,9 +43,9 @@ export default class JoyrideOverlay extends React.Component this.isActive = true; /* istanbul ignore else */ - if (!disableScrolling) { + if (process.env.NODE_ENV !== 'production') { /* istanbul ignore else */ - if (process.env.NODE_ENV === 'development' && hasCustomScrollParent(element, true)) { + if (!disableScrolling && hasCustomScrollParent(element, true)) { log({ title: 'step has a custom scroll parent and can cause trouble with scrolling', data: [{ key: 'parent', value: this.scrollParent }], diff --git a/src/components/index.tsx b/src/components/index.tsx index cba922ff..5dd924a0 100644 --- a/src/components/index.tsx +++ b/src/components/index.tsx @@ -236,10 +236,9 @@ class Joyride extends React.Component { const { index, lifecycle } = this.state; const { steps } = this.props; const step = steps[index]; - const intKey = window.Event ? event.which : event.keyCode; if (lifecycle === LIFECYCLE.TOOLTIP) { - if (intKey === 27 && step && !step.disableCloseOnEsc) { + if (event.code === 'Escape' && step && !step.disableCloseOnEsc) { this.store.close(); } } @@ -272,7 +271,7 @@ class Joyride extends React.Component { } = this.props; const step = getMergedStep(steps[index], this.props); - const target = getElement(step.target) as HTMLElement; + const target = getElement(step.target); const shouldScrollToStep = shouldScroll({ isFirstStep: index === 0, lifecycle, diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 00000000..f08f4fab --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,8 @@ +declare global { + namespace NodeJS { + interface ProcessEnv { + NODE_ENV: 'development' | 'production' | 'test'; + } + } +} +export {}; diff --git a/src/modules/dom.ts b/src/modules/dom.ts index ddb0ceb4..486825d8 100644 --- a/src/modules/dom.ts +++ b/src/modules/dom.ts @@ -196,7 +196,7 @@ export function getElementPosition( /** * Get the scrollTop position */ -export function getScrollTo(element: HTMLElement, offset: number, skipFix: boolean): number { +export function getScrollTo(element: HTMLElement | null, offset: number, skipFix: boolean): number { if (!element) { return 0; } diff --git a/src/modules/helpers.ts b/src/modules/helpers.ts index 7b10ea2c..5ebc92ce 100644 --- a/src/modules/helpers.ts +++ b/src/modules/helpers.ts @@ -25,7 +25,7 @@ interface ShouldScrollOptions { previousLifecycle: Lifecycle; scrollToFirstStep: boolean; step: Step; - target: HTMLElement; + target: HTMLElement | null; } export const isReact16 = createPortal !== undefined; diff --git a/src/modules/scope.ts b/src/modules/scope.ts index 1659e720..0c46c308 100644 --- a/src/modules/scope.ts +++ b/src/modules/scope.ts @@ -128,11 +128,11 @@ export default class Scope { return; } - const target = this.element.querySelector(selector) as HTMLElement; + const target = this.element.querySelector(selector); /* istanbul ignore else */ if (target) { - window.requestAnimationFrame(() => this.checkFocus(target)); + window.requestAnimationFrame(() => this.checkFocus(target as HTMLElement)); } }; } diff --git a/test/modules/scope.spec.tsx b/test/modules/scope.spec.tsx index 6987687a..baeb16ed 100644 --- a/test/modules/scope.spec.tsx +++ b/test/modules/scope.spec.tsx @@ -51,7 +51,6 @@ describe('modules/scope', () => { }); it('should not have created an instance', () => { - // expect(Scope).toHaveBeenCalledTimes(1); expect(scope).toBeUndefined(); }); });