Skip to content

Commit

Permalink
feat: complete refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammedFaragallah committed Jun 5, 2020
1 parent 7afbc11 commit 1dbeb7e
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 410 deletions.
74 changes: 60 additions & 14 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import React from 'react';

import { Box, CssBaseline } from '@material-ui/core';
import React from 'react';
import {
ReactIconsProvider,
defaultIcons,
ICON_DEFAULT_SIZE,
ReactIconsProvider,
SocialIcon,
SocialIconProps,
} from 'react-icons-context';
import { makeStyles } from '@material-ui/core';

const useStyles = makeStyles((theme) => {
const { transitions } = theme;
return {
root: {
transition: transitions.create('all', { duration: 500 }),
'&:hover': {
transform: 'scale(1.1)',
},
},
};
});

const mySocials: SocialIconProps[] = [
{ href: 'https://github.com/MohammedFaragallah', bgColor: '#333' },
Expand All @@ -15,9 +28,11 @@ const mySocials: SocialIconProps[] = [
{ href: 'https://instagram.com/mohammedalifaragallah', bgColor: '#E1306C' },
{ href: 'mailto:ffragalla@gmail.com', network: 'mailTo', bgColor: '#dd4b39' },
{
network: 'shareThis',
network: 'share',
fgColor: '#d7d8d9',
bgColor: 'tomato',
height: 120,
width: 80,
onClick: () =>
window.open(
'https://github.com/MohammedFaragallah/react-icons-context',
Expand All @@ -27,21 +42,52 @@ const mySocials: SocialIconProps[] = [
];

const App = () => {
const classes = useStyles();

return (
<ReactIconsProvider value={defaultIcons}>
<CssBaseline />
<Box
width={'100vw'}
height={'100vh'}
display={'flex'}
justifyContent={'center'}
alignItems={'center'}
width="100vw"
height="100vh"
display="flex"
justifyContent="center"
alignItems="center"
flexDirection="column"
>
{mySocials.map((network) => (
<Box mr={2} key={JSON.stringify(network)}>
<SocialIcon {...network} />
</Box>
))}
<Box display="flex" mb={4}>
{mySocials.map((network, index) => {
const props = {
target: '_blank',
size: ICON_DEFAULT_SIZE + (mySocials.length - index) * 10,
radius: (mySocials.length - index) * 8,
...network,
};

return (
<Box mr={2} key={JSON.stringify(network)}>
<SocialIcon
title={JSON.stringify(props, undefined, 2)}
classes={classes}
{...props}
/>
</Box>
);
})}
</Box>
<Box display="flex" flexWrap="wrap">
{Object.keys(defaultIcons).map((network) => (
<Box
key={network}
mr={2}
display="flex"
flexDirection="column"
alignItems="center"
>
<SocialIcon network={network} title={network} />
</Box>
))}
</Box>
</Box>
</ReactIconsProvider>
);
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
"eslint-plugin-import": "2.20.1"
},
"dependencies": {
"@material-ui/core": "^4.10.0"
"@material-ui/core": "^4.10.1"
},
"devDependencies": {
"@types/jest": "^25.2.3",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.8",
"@typescript-eslint/eslint-plugin": "^3.0.2",
"@typescript-eslint/parser": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^3.1.0",
"@typescript-eslint/parser": "^3.1.0",
"babel-eslint": "^10.1.0",
"cross-env": "^7.0.2",
"cz-conventional-changelog": "3.2.0",
Expand All @@ -71,14 +71,14 @@
"eslint-plugin-standard": "^4.0.1",
"gh-pages": "^3.0.0",
"husky": "^4.2.5",
"lint-staged": "^10.2.7",
"lint-staged": "^10.2.9",
"microbundle-crl": "^0.13.10",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "^3.4.1",
"release-it": "^13.6.1"
"release-it": "^13.6.2"
},
"peerDependencies": {
"react": "^16.13.1"
Expand Down
48 changes: 48 additions & 0 deletions src/Link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Link as MuiLink, makeStyles, Theme } from '@material-ui/core';
import React, { Children, cloneElement, ReactElement, FC } from 'react';

import { ReactIconsConsumer } from './context';
import { SocialIconLinkProps, IconProps, ClassesOverride } from './types';
import { keyFor } from './utils';

export const ICON_DEFAULT_SIZE = 33;

const useStyles = makeStyles<Theme, SocialIconLinkProps>({
root: ({ size = ICON_DEFAULT_SIZE, height = size, width = size }) => ({
width,
height,
display: 'inline-block',
position: 'relative',
overflow: 'hidden',
verticalAlign: 'middle',
}),
});

export type LinkClassesOverrides = ClassesOverride<typeof useStyles>;

interface Props extends SocialIconLinkProps {
classes?: ClassesOverride<typeof useStyles>;
}

export const Link: FC<Props> = (props) => {
const { href, network, classes: classesOverride, children, ...rest } = props;
const classes = useStyles(props);

const networkKey = (network || (href && keyFor(href)))?.toLowerCase();

return (
<ReactIconsConsumer>
{(icons) => {
const icon = (networkKey && icons[networkKey]) || icons.default;

return (
<MuiLink href={href} className={classes.root} {...rest}>
{Children.map(children, (child: ReactElement<IconProps>) =>
cloneElement(child, icon),
)}
</MuiLink>
);
}}
</ReactIconsConsumer>
);
};
17 changes: 17 additions & 0 deletions src/SocialIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { FC } from 'react';

import { Icon, IconClassesOverrides } from './Icon';
import { Link, LinkClassesOverrides } from './Link';
import { SocialIconProps } from './types';

interface Props extends SocialIconProps {
classes?: LinkClassesOverrides & IconClassesOverrides;
}

export const SocialIcon: FC<Props> = (props) => {
return (
<Link {...props}>
<Icon {...props} />
</Link>
);
};
9 changes: 0 additions & 9 deletions src/background.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createContext } from 'react';

import { defaultIcon } from './defaults';
import { defaultIcons } from './defaultIcons';
import { Icons } from './types';

export const ReactIconsContext = createContext<Icons>({ default: defaultIcon });
export const ReactIconsContext = createContext<Icons>(defaultIcons);

export const {
Provider: ReactIconsProvider,
Expand Down
7 changes: 3 additions & 4 deletions src/defaultIcons.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { defaultIcon } from './defaults';
import { email } from './icons/email';
import { facebook } from './icons/facebook';
import { github } from './icons/github';
import { google } from './icons/google';
import { instagram } from './icons/instagram';
import { shareThis } from './icons/shareThis';
import { share } from './icons/share';
import { twitter } from './icons/twitter';
import { whatsapp } from './icons/whatsapp';
import { youtube } from './icons/youtube';

export const defaultIcons = {
default: defaultIcon,
default: share,
mailto: email,
email,
shareThis,
share,
facebook,
github,
instagram,
Expand Down
5 changes: 0 additions & 5 deletions src/defaults.ts

This file was deleted.

69 changes: 52 additions & 17 deletions src/icon.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
import React, { CSSProperties, FC } from 'react';
import { makeStyles, Theme } from '@material-ui/core';
import React, { FC } from 'react';
import { ClassesOverride, IconProps } from './types';

import { socialSvgContent } from './styles';
import { Color, Icon as IconType } from './types';
const useStyles = makeStyles<Theme, Partial<IconProps>>((theme) => {
const { shape } = theme;
const variants = {
rounded: shape.borderRadius,
square: '0px',
circle: '50%',
};

interface StyleProps {
fgColor: Color;
}

const getStyle = ({ fgColor }: StyleProps): CSSProperties => ({
...socialSvgContent,
fill: fgColor || 'transparent',
return {
svg: ({ variant, radius }) => ({
borderRadius: radius || (variant && variants[variant]) || variants.circle,
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
fillRule: 'evenodd',
}),
background: {
fill: 'transparent',
},
mask: ({ color, bgColor = color }) => ({
fill: bgColor,
}),
icon: ({ fgColor }) => ({
fill: fgColor || 'transparent',
}),
};
});

interface Props extends StyleProps {
icon: IconType['icon'];
export type IconClassesOverrides = ClassesOverride<typeof useStyles>;

interface Props extends Partial<IconProps> {
classes?: IconClassesOverrides;
}

export const Icon: FC<Props> = ({ fgColor, icon, ...props }) => (
<g {...props} style={getStyle({ fgColor })}>
<path d={icon} />
</g>
);
export const Icon: FC<Props> = (props) => {
const classes = useStyles(props);
const { icon, mask } = props;

return (
<svg className={classes.svg} viewBox="0 0 64 64">
<g className={classes.background}>
<circle cx="32" cy="32" r="31" />
</g>
<g className={classes.icon}>
<path d={icon} />
</g>
<g className={classes.mask}>
<path d={mask} />
</g>
</svg>
);
};
2 changes: 1 addition & 1 deletion src/icons/shareThis.ts → src/icons/share.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const shareThis = {
export const share = {
icon:
'm28.388 32c0 0.0842-0.0192 0.1631-0.0242 0.247l8.8014 4.3993c0.7535-0.6381 1.7176-1.0364 2.7827-1.0364 2.3944 5e-4 4.3347 1.9409 4.3347 4.335 0 2.3968-1.9404 4.3371-4.3347 4.3371-2.3964 0-4.3347-1.9404-4.3347-4.3371 0-0.0863 0.0192-0.1634 0.024-0.247l-8.8014-4.3996c-0.7558 0.6357-1.7176 1.034-2.7827 1.034-2.3942 0-4.3347-1.9383-4.3347-4.3323 0-2.3965 1.9405-4.3373 4.3347-4.3373 1.065 0 2.0272 0.4007 2.7827 1.0364l8.8014-4.3995c-0.0048-0.0841-0.024-0.1632-0.024-0.2495 0-2.3918 1.9383-4.3323 4.3347-4.3323 2.3944 0 4.3347 1.9405 4.3347 4.3323 0 2.3964-1.9404 4.3373-4.3347 4.3373-1.0674 0-2.0296-0.4007-2.7827-1.0388l-8.8014 4.4019c0.0047 0.0841 0.0242 0.1632 0.0242 0.2495z',
mask:
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './context';
export * from './defaultIcons';
export * from './utils';
export * from './social-icon';
export * from './Icon';
export * from './Link';
export * from './SocialIcon';
export * from './types';
export * from './utils';
23 changes: 0 additions & 23 deletions src/mask.tsx

This file was deleted.

Loading

0 comments on commit 1dbeb7e

Please sign in to comment.