components: memoize and streamline callbacks

This commit is contained in:
rektdeckard
2020-09-14 01:05:55 -04:00
parent 1d862e6587
commit b4db1df589
6 changed files with 30 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
import React from "react";
import React, { useCallback } from "react";
import { useRecoilState, useRecoilValue } from "recoil";
import { iconColorAtom } from "../../state/atoms";
@@ -12,12 +12,15 @@ const ColorInput: React.FC<ColorInputProps> = () => {
const [color, setColor] = useRecoilState(iconColorAtom);
const isDark = useRecoilValue(isDarkThemeSelector);
const handleColorChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const {
target: { value: color },
} = event;
if (color[0] === "#") setColor(color);
};
const handleColorChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
const {
target: { value: color },
} = event;
if (color[0] === "#") setColor(color);
},
[setColor]
);
const throttledColorChange = useThrottled(handleColorChange, 100, [
handleColorChange,

View File

@@ -25,19 +25,17 @@ const illustrationVariants = {
visible: { opacity: 1, transition: { duration: 0.2 } },
};
const handleGetStarted = () => {
const handleGetStarted = () =>
window.open(
"https://github.com/phosphor-icons/phosphor-web#phosphor-icons",
"_blank",
"noopener noreferrer"
);
};
const handleScrollToIcons = () => {
const handleScrollToIcons = () =>
document
.getElementById("toolbar")
?.scrollIntoView({ behavior: "smooth", block: "start" });
};
const Header: React.FC<HeaderProps> = () => {
return (
@@ -62,7 +60,11 @@ const Header: React.FC<HeaderProps> = () => {
</button>
</div>
<div className="links">
<a className="nav-link" href="#">
<a
className="nav-link"
href={`${process.env.PUBLIC_URL}/favicon.ico`}
download
>
Download all
</a>
<a

View File

@@ -21,7 +21,6 @@ interface IconGridItemProps extends IconProps {
originOffset: MutableRefObject<{ top: number; left: number }>;
}
// const whileTap = { boxShadow: "0 0 0 6px rgba(163, 159, 171, 0.1)" };
const transition = { duration: 0.2 };
const originIndex = 0;
const delayPerPixel = 0.0004;
@@ -67,6 +66,7 @@ const IconGridItem: React.FC<IconGridItemProps> = (props) => {
const d = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
delayRef.current = d * delayPerPixel;
}, [originOffset]);
return (
<>
<motion.div

View File

@@ -29,6 +29,7 @@ const SearchInput: React.FC<SearchInputProps> = () => {
id="search-input"
aria-label="Search for an icon"
type="text"
autoCapitalize="off"
autoComplete="off"
value={value}
placeholder="Search"

View File

@@ -7,11 +7,12 @@ import "./SizeInput.css";
type SizeInputProps = {};
const handleFocus = (event: React.UIEvent<HTMLInputElement>) => {
const { currentTarget } = event;
currentTarget.focus();
event.preventDefault();
event.currentTarget.focus();
};
const handleBlur = (event: React.UIEvent<HTMLInputElement>) => {
event.preventDefault();
event.currentTarget.blur();
};

View File

@@ -1,5 +1,5 @@
import React from "react";
import { useRecoilState } from "recoil";
import { useSetRecoilState } from "recoil";
import Select from "react-dropdown-select";
import { PencilLine } from "phosphor-react";
@@ -7,7 +7,9 @@ import { iconStyleAtom } from "../../state/atoms";
import { IconStyle } from "../../lib";
import "./StyleInput.css";
const options = [
type WeightOption = { key: string; value: IconStyle; icon: JSX.Element };
const options: WeightOption[] = [
{
key: "Thin",
value: IconStyle.THIN,
@@ -43,12 +45,10 @@ const options = [
type StyleInputProps = {};
const StyleInput: React.FC<StyleInputProps> = () => {
const [style, setStyle] = useRecoilState(iconStyleAtom);
void style;
const setStyle = useSetRecoilState(iconStyleAtom);
// const handleStyleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
// setStyle(event.target.value as IconStyle);
// };
const handleStyleChange = (values: WeightOption[]) =>
setStyle(values[0].value as IconStyle);
return (
<Select
@@ -56,7 +56,7 @@ const StyleInput: React.FC<StyleInputProps> = () => {
values={[options[2]]}
searchable={false}
labelField="key"
onChange={(values) => setStyle(values[0].value as IconStyle)}
onChange={handleStyleChange}
itemRenderer={({
item,
itemIndex,