Skip to content

Commit 2ad7896

Browse files
committed
Further improve style accuracy
- Style videos & iframes correctly - Align some margins with Notion - Add breakpoint for responsive reflow
1 parent 057bfe3 commit 2ad7896

File tree

4 files changed

+90
-63
lines changed

4 files changed

+90
-63
lines changed

src/block.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import Asset from "./components/asset";
44
import Code from "./components/code";
55

66
export const renderChildText = (properties: DecorationType[]) => {
7-
if (!properties) return null;
87
return properties.map(([text, decorations], i) => {
98
if (!decorations) {
10-
return text;
9+
return <React.Fragment key={i}>{text}</React.Fragment>;
1110
}
1211

13-
return decorations.reduce((element, decorator) => {
12+
return decorations.reduceRight((element, decorator) => {
1413
switch (decorator[0]) {
1514
case "h":
1615
return (
@@ -78,7 +77,7 @@ export const Block: React.FC<Block> = props => {
7877
</h3>
7978
);
8079
case "divider":
81-
return <hr />;
80+
return <hr className="notion-hr" />;
8281
case "text":
8382
if (!blockValue.properties) {
8483
return <p style={{ height: "1rem" }}> </p>;
@@ -124,14 +123,17 @@ export const Block: React.FC<Block> = props => {
124123
const value = block.value as ContentValueType;
125124

126125
return (
127-
<div className="notion-asset-wrapper">
126+
<figure
127+
className="notion-asset-wrapper"
128+
style={{ width: value.format.block_width }}
129+
>
128130
<Asset block={block} />
129131
{value.properties.caption && (
130-
<div className="notion-image-caption">
132+
<figcaption className="notion-image-caption">
131133
{renderChildText(value.properties.caption)}
132-
</div>
134+
</figcaption>
133135
)}
134-
</div>
136+
</figure>
135137
);
136138
case "code": {
137139
if (blockValue.properties.title) {

src/components/asset.tsx

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from "react";
2-
import { CSSProperties } from "react";
32
import { BlockType, ContentValueType } from "../types";
43

54
const types = ["video", "image", "embed"];
@@ -14,26 +13,24 @@ const Asset: React.FC<{ block: BlockType }> = ({ block }) => {
1413

1514
const format = value.format;
1615
const {
17-
block_width,
18-
block_height,
1916
display_source,
20-
block_aspect_ratio
17+
block_aspect_ratio,
18+
block_height,
19+
block_width
2120
} = format;
2221

23-
const isImage = type === "image";
24-
25-
const style: CSSProperties = {
26-
maxWidth: "100%",
27-
border: "none",
28-
display: "block",
29-
marginLeft: "auto",
30-
marginRight: "auto",
31-
width: block_width,
32-
...(!isImage && { height: block_height })
33-
};
34-
35-
if (type === "embed") {
36-
return <iframe style={style} src={display_source} />;
22+
if (type === "embed" || type === "video") {
23+
return (
24+
<div
25+
style={{
26+
paddingBottom: `${(block_aspect_ratio || block_height / block_width) *
27+
100}%`,
28+
position: "relative"
29+
}}
30+
>
31+
<iframe className="notion-image-inset" src={display_source} />
32+
</div>
33+
);
3734
}
3835

3936
const src = `https://notion.so/image/${encodeURIComponent(
@@ -46,7 +43,6 @@ const Asset: React.FC<{ block: BlockType }> = ({ block }) => {
4643
return (
4744
<div
4845
style={{
49-
...style,
5046
paddingBottom: `${block_aspect_ratio * 100}%`,
5147
position: "relative"
5248
}}
@@ -55,14 +51,10 @@ const Asset: React.FC<{ block: BlockType }> = ({ block }) => {
5551
</div>
5652
);
5753
} else {
58-
return <img style={style} alt={caption} src={src} />;
54+
return <img alt={caption} src={src} />;
5955
}
6056
}
6157

62-
if (type === "video") {
63-
return <video style={style} src={src} />;
64-
}
65-
6658
return null;
6759
};
6860

src/components/code.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ const Code: React.FC<{ code: string; language: string }> = ({
1010
languages[language.toLowerCase()] || languages.javascript;
1111

1212
return (
13-
<pre>
14-
<code
15-
className="notion-code"
16-
dangerouslySetInnerHTML={{
17-
__html: highlight(code, prismLanguage, language)
18-
}}
19-
/>
20-
</pre>
13+
<code
14+
className="notion-code"
15+
dangerouslySetInnerHTML={{
16+
__html: highlight(code, prismLanguage, language)
17+
}}
18+
/>
2119
);
2220
};
2321

src/styles.css

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
padding: 3px 0px;
1010
}
1111

12-
* {
12+
.notion * {
1313
margin-block-start: 0px;
1414
margin-block-end: 0px;
1515
}
@@ -134,21 +134,22 @@ h3 {
134134
white-space: pre-wrap;
135135
word-break: break-word;
136136
border-left: 3px solid currentcolor;
137-
padding-left: 0.9em;
138-
padding-right: 0.9em;
137+
padding: 0.2em 0.9em;
138+
margin: 0;
139139
font-size: 1.2em;
140140
}
141141
.notion-hr {
142142
margin: 6px 0px;
143+
padding: 0;
144+
border-top: none;
143145
border-color: rgba(55, 53, 47, 0.09);
144146
}
145-
.notion-column {
146-
padding-top: 12px;
147-
padding-bottom: 12px;
148-
}
149147
.notion-link {
148+
color: inherit;
150149
text-decoration: underline;
150+
text-decoration-color: inherit;
151151
}
152+
152153
.notion-inline-code {
153154
color: #eb5757;
154155
padding: 0.2em 0.4em;
@@ -186,17 +187,19 @@ h3 {
186187
padding-left: 0.2em;
187188
}
188189

189-
li {
190+
.notion-list li {
190191
padding: 6px 0px;
191192
}
192193

193194
.notion-asset-wrapper {
194-
display: flex;
195-
justify-content: center;
196-
flex-direction: column;
197-
margin-top: 2px;
198-
margin-bottom: 0.5em;
195+
margin: 2px auto 0.5rem;
196+
max-width: 100%;
197+
}
198+
199+
.notion-asset-wrapper iframe {
200+
border: none;
199201
}
202+
200203
.notion-text {
201204
white-space: pre-wrap;
202205
word-break: break-word;
@@ -205,9 +208,50 @@ li {
205208
.notion-block {
206209
padding: 3px 2px;
207210
}
211+
212+
.notion-code {
213+
padding: 30px 16px 30px 20px;
214+
margin: 4px 0;
215+
tab-size: 2;
216+
font-size: 85%;
217+
display: block;
218+
background: rgb(247, 246, 243);
219+
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier,
220+
monospace;
221+
}
222+
223+
.notion-column {
224+
padding-top: 12px;
225+
padding-bottom: 12px;
226+
}
227+
228+
.notion-column > *:first-child {
229+
margin-top: 0;
230+
margin-left: 0;
231+
margin-right: 0;
232+
}
233+
234+
.notion-column > *:last-child {
235+
margin-left: 0;
236+
margin-right: 0;
237+
margin-bottom: 0;
238+
}
239+
208240
.notion-row {
209241
display: flex;
210242
}
243+
244+
@media (max-width: 640px) {
245+
.notion-row {
246+
flex-direction: column;
247+
}
248+
249+
.notion-row > *,
250+
.notion-column > * {
251+
width: 100% !important;
252+
}
253+
}
254+
211255
.notion-spacer:last-child {
212256
display: none;
213257
}
@@ -219,6 +263,7 @@ li {
219263
right: 0;
220264
bottom: 0;
221265
width: 100%;
266+
height: 100%;
222267
}
223268

224269
.notion-image-caption {
@@ -245,13 +290,3 @@ li {
245290
.notion-callout-text {
246291
margin-left: 8px;
247292
}
248-
.notion-code {
249-
padding: 30px 16px 30px 20px;
250-
margin: 4px 0;
251-
tab-size: 2;
252-
font-size: 85%;
253-
display: block;
254-
background: rgb(247, 246, 243);
255-
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier,
256-
monospace;
257-
}

0 commit comments

Comments
 (0)