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

feat(component): add Image component #50

Merged
merged 5 commits into from
Jul 11, 2022
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
1 change: 0 additions & 1 deletion src/components/Column/Column.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const Default = Template.bind({});

Default.args = {
children: <p style={{ margin: '0', fontSize: '30px' }}>Hello World</p>,
align: 'center',
classes: {
root: {
backgroundColor: 'red',
Expand Down
11 changes: 7 additions & 4 deletions src/components/Column/Column.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ReactNode } from 'react';
import { BaseStyleProp } from '../types';
import { CSSProperties, ReactNode } from 'react';
import { makeStyles } from '../../utils/makeStyles';
import { sx } from '../../utils/sx';
import { BaseStyleProp } from '../types';

type ColumnStyles = 'root';

export interface ColumnProps extends BaseStyleProp<ColumnStyles> {
children?: ReactNode;
align?: 'left' | 'center' | 'right';
verticalAlign?: CSSProperties['verticalAlign'];
}

const useStyles = makeStyles({
Expand All @@ -17,12 +19,13 @@ export const Column = ({
children,
className,
classes,
align = 'left',
align = 'center',
verticalAlign = 'top',
}: ColumnProps): JSX.Element => {
const styles = useStyles({ classes });

return (
<td className={className} style={styles.root} align={align}>
<td className={className} style={sx(styles.root, { verticalAlign })} align={align}>
{children}
</td>
);
Expand Down
51 changes: 51 additions & 0 deletions src/components/Image/Image.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ComponentStory, ComponentMeta } from '@storybook/react';

import { Email } from '../Email/Email';
import { Section } from '../Section/Section';
import { Column } from '../Column/Column';
import { Typography } from '../Typography/Typography';
import { Image } from './Image';
import { Divider } from '../Divider/Divider';

export default {
component: Image,
} as ComponentMeta<typeof Image>;

//“template” of how args map to rendering
const Template: ComponentStory<typeof Image> = (args) => (
<Email>
<Section>
<Column>
<Typography variant="h2" align="center">
Hello World
</Typography>
</Column>
</Section>

<Divider />

<Section fullWidth={false}>
<Column>
<Image {...args} />
</Column>

<Column classes={{ root: { paddingLeft: '20px' } }}>
<Typography variant="h2">This is a random text</Typography>
</Column>
</Section>
</Email>
);

export const Default = Template.bind({});

Default.args = {
src: 'https://images.unsplash.com/photo-1453728013993-6d66e9c9123a',
alt: 'This is a random image',
width: '250px',
caption: 'This is a caption',
classes: {
image: {
borderRadius: '10px',
},
},
};
78 changes: 78 additions & 0 deletions src/components/Image/Image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { CSSProperties } from 'react';
import { makeStyles } from '../../utils/makeStyles';
import { sx } from '../../utils/sx';
import { BaseStyleProp } from '../types';

type ImageStyles =
| 'root'
| 'table'
| 'body'
| 'image'
| 'caption'
| 'imageSection'
| 'captionSection'
| 'imageColumn';

export interface ImageProps extends BaseStyleProp<ImageStyles> {
src: string;
alt: string;
caption?: string;
width: CSSProperties['width'];
height?: CSSProperties['height'];
captionAlign?: 'left' | 'center' | 'right';
}

const useStyles = makeStyles({
root: { border: '0', display: 'block', outline: 'none' },
table: { width: 'fit-content', border: '0', verticalAlign: 'top' },
body: {},
image: {},
caption: {},
imageSection: {},
captionSection: {},
imageColumn: {},
});

export const Image = ({
src,
alt,
caption,
width,
height = 'auto',
classes,
className,
captionAlign = 'center',
}: ImageProps): JSX.Element => {
const styles = useStyles({ classes });

return caption ? (
<table cellPadding="0" cellSpacing="0" role="presentation" style={styles.table}>
<tbody style={styles.body}>
<tr style={styles.imageSection}>
<td style={styles.imageColumn}>
<img
className={className}
height={height}
width={width}
src={src}
alt={alt}
style={sx(styles.root, styles.image)}
/>
</td>
</tr>
<tr style={styles.captionSection}>
<td style={sx(styles.caption, { textAlign: captionAlign })}>{caption}</td>
</tr>
</tbody>
</table>
) : (
<img
className={className}
height={height}
width={width}
src={src}
alt={alt}
style={sx(styles.root, styles.image)}
/>
);
};
1 change: 1 addition & 0 deletions src/components/Image/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Image, ImageProps } from './Image';
13 changes: 10 additions & 3 deletions src/components/Section/Section.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
import { ReactNode } from 'react';
import { makeStyles } from '../../utils/makeStyles';
import { sx } from '../../utils/sx';
import { BaseStyleProp } from '../types';

type SectionStyles = 'root' | 'body' | 'row';

export interface SectionProps extends BaseStyleProp<SectionStyles> {
children?: ReactNode;
fullWidth?: boolean;
}

const useStyles = makeStyles({
root: { width: '100%', border: '0', verticalAlign: 'top' },
root: { border: '0', verticalAlign: 'top' },
body: {},
row: {},
});

export const Section = ({ children, className, classes }: SectionProps): JSX.Element => {
export const Section = ({
children,
className,
classes,
fullWidth = true,
}: SectionProps): JSX.Element => {
const styles = useStyles({ classes });

return (
<table
cellPadding="0"
cellSpacing="0"
role="presentation"
style={styles.root}
style={sx(styles.root, fullWidth && { width: '100%' })}
className={className}
>
<tbody style={styles.body}>
Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './Button';
export * from './Link';
export * from './Divider';
export * from './Typography';
export * from './Image';