Skip to content

Commit ab7dc6a

Browse files
authored
Merge pull request #59 from code-hike/optional-browser-transition
Optional browser transition
2 parents bf79a22 + 324d15c commit ab7dc6a

File tree

5 files changed

+70
-27
lines changed

5 files changed

+70
-27
lines changed

packages/mini-browser/src/buttons.tsx

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
import React from "react";
1+
import { useClasser } from "@code-hike/classer"
2+
import React from "react"
23

34
function Back() {
5+
const c = useClasser("ch-browser")
46
return (
57
<svg
68
fill="currentColor"
79
preserveAspectRatio="xMidYMid meet"
810
height="1em"
911
viewBox="13 10 14 23"
10-
style={{ marginLeft: "0.2em", marginRight: "1em", color: "#999" }}
12+
className={c("button", "back-button")}
1113
>
1214
<g>
1315
<path d="m26.5 12.1q0 0.3-0.2 0.6l-8.8 8.7 8.8 8.8q0.2 0.2 0.2 0.5t-0.2 0.5l-1.1 1.1q-0.3 0.3-0.6 0.3t-0.5-0.3l-10.4-10.4q-0.2-0.2-0.2-0.5t0.2-0.5l10.4-10.4q0.3-0.2 0.5-0.2t0.6 0.2l1.1 1.1q0.2 0.2 0.2 0.5z" />
1416
</g>
1517
</svg>
16-
);
18+
)
1719
}
1820

1921
function Forward() {
22+
const c = useClasser("ch-browser")
2023
return (
2124
<svg
2225
fill="currentColor"
2326
preserveAspectRatio="xMidYMid meet"
2427
height="1em"
2528
viewBox="13 10 14 23"
26-
style={{ marginRight: "1em", color: "#999" }}
29+
className={c("button", "forward-button")}
2730
>
2831
<g>
2932
<path d="m26.3 21.4q0 0.3-0.2 0.5l-10.4 10.4q-0.3 0.3-0.6 0.3t-0.5-0.3l-1.1-1.1q-0.2-0.2-0.2-0.5t0.2-0.5l8.8-8.8-8.8-8.7q-0.2-0.3-0.2-0.6t0.2-0.5l1.1-1.1q0.3-0.2 0.5-0.2t0.6 0.2l10.4 10.4q0.2 0.2 0.2 0.5z" />
3033
</g>
3134
</svg>
32-
);
35+
)
3336
}
3437

3538
function Refresh() {
@@ -43,13 +46,14 @@ function Refresh() {
4346
>
4447
<path d="M29.5 10.5l3.9-3.9v11.8H21.6L27 13c-1.8-1.8-4.3-3-7-3-5.5 0-10 4.5-10 10s4.5 10 10 10c4.4 0 8.1-2.7 9.5-6.6h3.4c-1.5 5.7-6.6 10-12.9 10-7.3 0-13.3-6.1-13.3-13.4S12.7 6.6 20 6.6c3.7 0 7 1.5 9.5 3.9z" />
4548
</svg>
46-
);
49+
)
4750
}
4851

4952
function Open({ href }: { href: string }) {
53+
const c = useClasser("ch-browser")
5054
return (
5155
<a
52-
style={{ margin: "0 1em 0 1em", color: "inherit" }}
56+
className={c("button", "open-button")}
5357
title="Open in new tab"
5458
href={href}
5559
target="_blank"
@@ -62,13 +66,13 @@ function Open({ href }: { href: string }) {
6266
viewBox="3 3 18 18"
6367
height="1em"
6468
width="1em"
65-
style={{ display: "block" }}
69+
className={c("open-icon")}
6670
xmlns="http://www.w3.org/2000/svg"
6771
>
6872
<path d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z"></path>
6973
</svg>
7074
</a>
71-
);
75+
)
7276
}
7377

74-
export { Back, Forward, Refresh, Open };
78+
export { Back, Forward, Refresh, Open }

packages/mini-browser/src/index.scss

+20
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,23 @@
2222
padding: 0 10px;
2323
color: #544;
2424
}
25+
26+
.ch-browser-button {
27+
margin: 0 1em;
28+
color: #999;
29+
}
30+
31+
.ch-browser-back-button {
32+
margin-left: 0.2em;
33+
}
34+
35+
.ch-browser-forward-button {
36+
margin-left: 0;
37+
}
38+
39+
.ch-browser-open-button {
40+
color: inherit;
41+
}
42+
.ch-browser-open-icon {
43+
display: block;
44+
}

packages/mini-browser/src/mini-browser-hike.tsx

+25-12
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import { TitleBar } from "./title-bar"
44
import { MiniBrowserStep, useSteps } from "./use-steps"
55
import { useClasser, Classes } from "@code-hike/classer"
66

7+
type Transition = "none" | "slide"
8+
79
export type MiniBrowserHikeProps = {
810
progress?: number
911
backward?: boolean
1012
classes?: Classes
1113
steps?: MiniBrowserStep[]
14+
transition?: Transition
1215
} & React.PropsWithoutRef<JSX.IntrinsicElements["div"]>
1316

1417
const MiniBrowserHike = React.forwardRef<
@@ -21,6 +24,7 @@ function MiniBrowserWithRef(
2124
progress = 0,
2225
backward = false,
2326
steps: ogSteps,
27+
transition = "none",
2428
classes,
2529
...props
2630
}: MiniBrowserHikeProps,
@@ -29,16 +33,6 @@ function MiniBrowserWithRef(
2933
const c = useClasser("ch-mini-browser", classes)
3034
const steps = useSteps(ogSteps)
3135

32-
// TODO readability and optional
33-
const X = 50
34-
const t = progress - Math.floor(progress)
35-
const x = t <= 0.5 ? -X * t : X - X * t
36-
const o = Math.abs(t - 0.5) * 2
37-
38-
// const stepIndex = backward
39-
// ? Math.floor(progress)
40-
// : Math.ceil(progress)
41-
4236
const stepIndex = Math.round(progress)
4337

4438
const { zoom, displayUrl, loadUrl, children } = steps[
@@ -51,8 +45,7 @@ function MiniBrowserWithRef(
5145
zoom={zoom}
5246
className={`${c("")} ${props.className || ""}`}
5347
style={{
54-
transform: `translateX(${x}px)`,
55-
opacity: o * o,
48+
...transitionStyle({ progress, transition }),
5649
...props.style,
5750
}}
5851
classes={classes}
@@ -65,4 +58,24 @@ function MiniBrowserWithRef(
6558
)
6659
}
6760

61+
function transitionStyle({
62+
progress,
63+
transition,
64+
}: {
65+
progress: number
66+
transition: Transition
67+
}) {
68+
if (transition === "slide") {
69+
const X = 50
70+
const t = progress - Math.floor(progress)
71+
const x = t <= 0.5 ? -X * t : X - X * t
72+
const o = Math.abs(t - 0.5) * 2
73+
return {
74+
transform: `translateX(${x}px)`,
75+
opacity: o * o,
76+
}
77+
}
78+
return {}
79+
}
80+
6881
export { MiniBrowserHike }

packages/scrollycoding/src/mdx-steps.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ interface Step {
2525
codeProps: CodeProps
2626
}
2727

28-
const defaultFilename = "App.js"
28+
interface Options {
29+
defaultFileName?: string
30+
}
2931

3032
function useMdxSteps(
3133
mdx: React.ReactNode,
3234
previewProps: PreviewProps,
33-
codeProps: CodeProps
35+
codeProps: CodeProps,
36+
options: Options = {}
3437
) {
3538
const steps: Step[] = []
3639
let prevFiles: CodeFiles = {}
@@ -41,7 +44,8 @@ function useMdxSteps(
4144
const { files, activeFile } = getFiles(
4245
stepHeadProps,
4346
prevFiles,
44-
prevActiveFile
47+
prevActiveFile,
48+
options.defaultFileName
4549
)
4650
const step = {
4751
content: [],
@@ -69,7 +73,8 @@ function useMdxSteps(
6973
function getFiles(
7074
stepHeadProps: StepHeadProps,
7175
prevFiles: CodeFiles = {},
72-
prevActiveFile: string = ""
76+
prevActiveFile: string = "",
77+
defaultFileName: string = "App.js"
7378
) {
7479
let activeFile = stepHeadProps.activeFile || ""
7580
const files = { ...prevFiles }
@@ -80,7 +85,7 @@ function getFiles(
8085
preElement?.props?.children?.props || {}
8186
const lang = codeElementProps.className?.slice(9)
8287
const { filename, hideTab } = parseMetastring(
83-
codeElementProps.metastring || defaultFilename
88+
codeElementProps.metastring || defaultFileName
8489
)
8590
const code = codeElementProps.children
8691
files[filename] = { code, lang, hideTab }

packages/scrollycoding/src/preview.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function Preview({ filesHash, ...props }: PreviewProps) {
3434
<MiniBrowser
3535
url=""
3636
loadUrl={link}
37+
transition="slide"
3738
{...props}
3839
children={preview}
3940
/>

0 commit comments

Comments
 (0)