Skip to content

Commit

Permalink
chore: update document type check
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 21, 2020
1 parent 86d2da3 commit 19fc530
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 36 deletions.
5 changes: 1 addition & 4 deletions examples/custom-pages-storybook-5/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { Person } from './components/Person';

ReactDOM.render(
<Person />,
document.getElementById("root")
);
ReactDOM.render(<Person />, document.getElementById('root'));
5 changes: 1 addition & 4 deletions examples/custom-pages-storybook-6/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { Person } from './components/Person';

ReactDOM.render(
<Person />,
document.getElementById("root")
);
ReactDOM.render(<Person />, document.getElementById('root'));
2 changes: 1 addition & 1 deletion examples/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"@component-controls/gatsby-theme-stories": "^1.34.0",
"gatsby": "^2.22.10",
"gatsby": "^2.23.11",
"react": "^16.13.1",
"react-dom": "^16.13.1"
}
Expand Down
4 changes: 3 additions & 1 deletion ui/blocks/src/PageContainer/PageContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export const PageContainer: FC<PageContainerProps> = forwardRef(
if (url) {
const scrollId = url.hash ? url.hash.substring(1) : undefined;
if (scrollId) {
const element = document.getElementById(scrollId);
const element =
typeof document !== 'undefined' &&
document.getElementById(scrollId);
if (element) {
setTimeout(() => {
element.scrollIntoView({
Expand Down
16 changes: 8 additions & 8 deletions ui/blocks/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const getURL = () => {
const pageURL =
(typeof window !== 'undefined' &&
window.location !== window.parent.location &&
window.parent.location
? window.parent.location.href
: typeof document !== 'undefined'
? document.location.href
: undefined) || undefined;
let pageURL = undefined;
if (typeof window !== 'undefined') {
if (window.location !== window.parent.location && window.parent.location) {
pageURL = window.parent.location.href;
} else {
pageURL = document.location.href;
}
}
return pageURL ? new URL(pageURL) : undefined;
};
16 changes: 4 additions & 12 deletions ui/components/src/Keyboard/Keyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,16 @@ export const Keyboard: FC<KeyboardProps> = ({
);

useEffect(() => {
if (target === 'document' && typeof document !== 'undefined' && document) {
if (target === 'document' && typeof document !== 'undefined') {
document.addEventListener('keydown', onKeyDownFn);
} else if (target === 'window' && typeof window !== 'undefined' && window) {
} else if (target === 'window' && typeof window !== 'undefined') {
window.addEventListener('keydown', onKeyDownFn);
}

return () => {
if (
target === 'document' &&
typeof document !== 'undefined' &&
document
) {
if (target === 'document' && typeof document !== 'undefined') {
document.removeEventListener('keydown', onKeyDownFn);
} else if (
target === 'window' &&
typeof window !== 'undefined' &&
window
) {
} else if (target === 'window' && typeof window !== 'undefined') {
window.removeEventListener('keydown', onKeyDownFn);
}
};
Expand Down
12 changes: 8 additions & 4 deletions ui/components/src/LinkHeading/pageLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ export const titleToId = (id: any) => {
return strId.replace(/\W/g, '-').toLowerCase();
};
export const pageLink = (id: string) => {
const url =
(typeof window !== 'undefined' && window.location !== window.parent.location
? document.referrer
: typeof document !== 'undefined' && document.location.href) || '';
let url = '';
if (typeof window !== 'undefined') {
if (window.location !== window.parent.location) {
url = document.referrer;
} else {
url = document.location.href || '';
}
}
return `${url.split('#')[0]}#${id}`;
};
8 changes: 6 additions & 2 deletions ui/components/src/SearchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ export const SearchInput = <ItemType extends SearchInputItemType>({
const downIndex = Math.min((selected || -1) + 1, items.length - 1);
if (downIndex >= 0) {
selectItem(items[downIndex], downIndex, false);
const itemEl = document.getElementById(`search_item_${downIndex}`);
const itemEl =
typeof document !== 'undefined' &&
document.getElementById(`search_item_${downIndex}`);
if (itemEl) {
scrollIntoView(itemEl, { block: 'end', scrollMode: 'if-needed' });
}
Expand All @@ -122,7 +124,9 @@ export const SearchInput = <ItemType extends SearchInputItemType>({
const upIndex = Math.max((selected || items.length) - 1, 0);
if (upIndex < items.length) {
selectItem(items[upIndex], upIndex, false);
const itemEl = document.getElementById(`search_item_${upIndex}`);
const itemEl =
typeof document !== 'undefined' &&
document.getElementById(`search_item_${upIndex}`);
if (itemEl) {
scrollIntoView(itemEl, { block: 'start', scrollMode: 'if-needed' });
}
Expand Down

0 comments on commit 19fc530

Please sign in to comment.