Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace calls to getWindow with a simpler check #216

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/components/control-text/control-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import ControlLabel from '../control-label';
import ControlWrapper from '../control-wrapper';
import Popover from '../popover';
import Icon from '../icon';
import getWindow from '../utils/get-window';
import {InputProps, PopoverProps} from '../typings';

interface Props extends Omit<InputProps, 'onChange'> {
Expand Down Expand Up @@ -71,9 +70,11 @@ export default function ControlText({
};

const isActive = () => {
if (typeof window === 'undefined') return false

return (
getWindow().document.activeElement === inputElement.current ||
getWindow().document.activeElement === errorElement.current
window.document.activeElement === inputElement.current ||
window.document.activeElement === errorElement.current
);
};

Expand Down
24 changes: 14 additions & 10 deletions src/components/copiable/copiable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import CopyButton from '../copy-button';
import Popover from '../popover';
import OSKey from 'os-key';
import select from 'select';
import getWindow from '../utils/get-window';

const DISABLE_CLICK_TO_SELECT_THRESHOLD = 640;
const FEEDBACK_TIME = 2000;
Expand Down Expand Up @@ -59,7 +58,8 @@ export default function Copiable({
}, [copyTooltipActive]);

const handleTextFocus = () => {
if (getWindow().innerWidth < DISABLE_CLICK_TO_SELECT_THRESHOLD) return;
if (typeof window === 'undefined') return
if (window.innerWidth < DISABLE_CLICK_TO_SELECT_THRESHOLD) return;
select(() => textEl.current);
setCopyTooltipActive(true);
};
Expand All @@ -80,14 +80,18 @@ export default function Copiable({
</div>
);

const renderCopyHintText = (
<span>
<span className="txt-kbd">
{getCopyKeys(getWindow().navigator.userAgent)}
</span>{' '}
to copy
</span>
);
const renderCopyHintText = () => {
if (typeof window === 'undefined') return

return (
<span>
<span className="txt-kbd">
{getCopyKeys(window.navigator.userAgent)}
</span>{' '}
to copy
</span>
)
}

const textClasses = classnames('my3 txt-mono txt-s mr24', {
'txt-truncate': truncated
Expand Down
123 changes: 0 additions & 123 deletions src/components/icon/__tests__/__snapshots__/icon.test.tsx.snap

This file was deleted.

96 changes: 0 additions & 96 deletions src/components/icon/__tests__/icon.test.tsx

This file was deleted.

15 changes: 4 additions & 11 deletions src/components/icon/icon.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import getWindow from '../utils/get-window';
import Icon from './icon';

jest.mock('../utils/get-window');

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getWindowMock = getWindow as any;

describe('basic', () => {
test('renders', () => {
const { baseElement } = render(<Icon name='bike' />)
Expand Down Expand Up @@ -52,12 +46,11 @@ describe('basic', () => {

describe('inline', () => {
beforeEach(() => {
const mockWindow = {
getComputedStyle: jest.fn(() => ({
Object.defineProperty(window, 'getComputedStyle', {
value: () => ({
'line-height': '14px'
}))
};
getWindowMock.mockReturnValue(mockWindow);
})
});
});

test('renders', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { ReactElement, useRef, useEffect, SVGProps, DetailedHTMLProps } from 'react';
import PropTypes from 'prop-types';
import getWindow from '../utils/get-window';
import * as AccessibleIcon from '@radix-ui/react-accessible-icon';

interface Props {
Expand Down Expand Up @@ -28,8 +27,8 @@ export default function Icon({
const el = useRef(null);

useEffect(() => {
if (inline && el.current) {
const lineHeight = getWindow().getComputedStyle(el.current)[
if (inline && el.current && typeof window !== 'undefined') {
const lineHeight = window.getComputedStyle(el.current)[
'line-height'
];
el.current.style.height = lineHeight;
Expand Down
8 changes: 0 additions & 8 deletions src/components/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

All utils can be imported from `@mapbox/mr-ui/utils/{name}`.

## getWindow

```js
import getWindow from '@mapbox/mr-ui/utils/get-window';
```

Wraps `window` to make it easy to mock aspects of the browser environment while testing.

## shallowEqualObjects

```js
Expand Down
9 changes: 0 additions & 9 deletions src/components/utils/get-window.ts

This file was deleted.

Loading