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

fix: update svg packages, migrate metadata, lazy load svg components #970

Merged
merged 17 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: perf enhancements
  • Loading branch information
vpicone committed Mar 4, 2020
commit 64d9ed3d6a8f751b2344b766c4b306bf29e484eb
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"rules": {
"react/prop-types": 0,
"react/forbid-prop-types": 0,
"arrow-body-style": 0
"arrow-body-style": 0,
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
},
"globals": {
"fathom": "readonly"
Expand Down
20 changes: 9 additions & 11 deletions plugins/gatsby-theme-carbon-svgs/components/shared/ActionBar.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import ReactDOMServer from 'react-dom/server';
import React, { useRef, useContext, useState, useEffect } from 'react';
import React, { useRef, useContext, useState } from 'react';
import { pascalCase } from 'change-case';
import { Code16, Download16 } from '@carbon/icons-react';
import { TooltipDefinition } from 'carbon-components-react';
import copy from 'copy-to-clipboard';
import cx from 'classnames';
import { LibraryContext } from './LibraryProvider';
import styles from './ActionBar.module.scss';

Expand Down Expand Up @@ -43,23 +44,20 @@ const ActionBar = ({

const handleCopy = () => {
setCopyText('Copied!');
setTimeout(() => {
setCopyText(`Copy ${component}`);
}, 2000);
copy(component);
};

useEffect(() => {
if (copyText === 'Copied!') {
setTimeout(() => {
setCopyText(`Copy ${component}`);
}, 2000);
}
}, [copyText]);

return (
<div
ref={actionBarRef}
onBlur={handleBlurEvent}
hidden={!isActionBarVisible}
className={styles.container}>
aria-hidden={!isActionBarVisible}
className={cx(styles.container, {
[styles.hidden]: !isActionBarVisible,
})}>
<TooltipDefinition
onFocus={() => setIsActionBarVisible(true)}
onClick={handleDownload}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
bottom: 0;
right: 0;
opacity: 1;
transition: opacity $duration--fast-02;
transition: opacity $duration--fast-01 motion(standard, productive);
z-index: 1;
svg {
fill: $icon-01;
}
}

.container[hidden] {
.container.hidden {
opacity: 0;
}

Expand Down
34 changes: 18 additions & 16 deletions plugins/gatsby-theme-carbon-svgs/components/shared/SvgCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@ const SvgCard = ({ icon, containerIsVisible, ...rest }) => {
<div className={svgCardInside}>
<span className={triggerText}>{friendlyName}</span>
{containerIsVisible && (
<div className={flexContainer}>
{Component ? (
<Component {...rest}>
<title>{friendlyName}</title>
</Component>
) : (
<p>Error: no component found for {pascalCase(friendlyName)}</p>
)}
</div>
<>
<div className={flexContainer}>
{Component ? (
<Component {...rest}>
<title>{friendlyName}</title>
</Component>
) : (
<p>Error: no component found for {pascalCase(friendlyName)}</p>
)}
</div>
<ActionBar
name={name}
component={Component}
friendlyName={friendlyName}
isActionBarVisible={isActionBarVisible}
setIsActionBarVisible={setIsActionBarVisible}
/>
</>
)}
<ActionBar
name={name}
component={Component}
friendlyName={friendlyName}
isActionBarVisible={isActionBarVisible}
setIsActionBarVisible={setIsActionBarVisible}
/>
</div>
</li>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
.svg-page {
margin-left: -$spacing-05;
margin-right: -$spacing-05;

// prevent jank when icons load
min-height: 60vh;
}

.filter-row {
Expand Down Expand Up @@ -283,7 +280,7 @@ $svg-card-border: 1px solid $ui-03;

.flex-container svg {
fill: $icon-01;
animation: fadeIn $duration--fast-02 motion(standard, productive);
animation: fadeIn $duration--fast-01 motion(standard, productive);
}

@keyframes fadeIn {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import { useRef, useEffect, useState, useCallback } from 'react';
import { useRef, useLayoutEffect, useState, useCallback } from 'react';

const useIntersectionObserver = () => {
const targetRef = useRef();
const observerRef = useRef();

const [node, setNode] = useState(null);
const [visible, setVisible] = useState(false);
const observerRef = useRef();

const callback = useCallback(
([entry]) => {
if (entry.intersectionRatio) {
setVisible(true);
}
},
[setVisible]
);

useEffect(() => {
if (observerRef.current) return;
if (!targetRef.current) return;

observerRef.current = new IntersectionObserver(callback, {
rootMargin: '50%',
});

observerRef.current.observe(targetRef.current);
const disconnect = useCallback(() => {
const { current } = observerRef;
if (current) {
current.disconnect();
}
}, []);

return [targetRef, visible];
const observe = useCallback(() => {
observerRef.current = new IntersectionObserver(
([entry]) => setVisible(entry.isIntersecting),
{
rootMargin: '75%',
}
);
if (node) {
observerRef.current.observe(node);
}
}, [node]);

useLayoutEffect(() => {
observe();
return () => {
disconnect();
};
}, [observe, disconnect]);

return [setNode, visible];
};

export default useIntersectionObserver;