Skip to content

Commit d3183ba

Browse files
committed
Add overflow scrollbars to mini-editor
1 parent ed85b71 commit d3183ba

File tree

7 files changed

+54
-18
lines changed

7 files changed

+54
-18
lines changed

packages/mini-editor/src/code.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type CodeProps = {
1616
language: string
1717
parentHeight?: number
1818
minColumns: number
19+
minZoom: number
20+
maxZoom: number
1921
}
2022

2123
export function Code({
@@ -27,6 +29,8 @@ export function Code({
2729
language,
2830
parentHeight,
2931
minColumns,
32+
minZoom,
33+
maxZoom,
3034
}: CodeProps) {
3135
const {
3236
prevLines,
@@ -53,17 +57,7 @@ export function Code({
5357
<pre
5458
ref={ref}
5559
className={`language-${language}`}
56-
style={{
57-
position: "absolute",
58-
top: 0,
59-
bottom: 0,
60-
right: 16,
61-
left: 16,
62-
padding: 0,
63-
margin: 0,
64-
overflow: "hidden",
65-
opacity: dimensions ? 1 : 0,
66-
}}
60+
style={{ opacity: dimensions ? 1 : 0 }}
6761
>
6862
<code>
6963
{dimensions ? (
@@ -85,7 +79,8 @@ export function Code({
8579
}
8680
prevFocus={prevFocusIndexes}
8781
nextFocus={nextFocusIndexes}
88-
maxZoom={1}
82+
minZoom={minZoom}
83+
maxZoom={maxZoom}
8984
/>
9085
) : (
9186
<>

packages/mini-editor/src/index.scss

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,22 @@
4343
height: 100%;
4444
color: #cccccc;
4545
font-size: 15px;
46-
padding: 5px 10px;
46+
// padding: 5px 10px;
4747
line-height: 1.1rem;
4848
box-sizing: border-box;
4949
}
5050

51+
.ch-editor-body pre {
52+
position: absolute;
53+
top: 0;
54+
bottom: 0;
55+
left: 0;
56+
right: 0;
57+
padding: 0 16px;
58+
margin: 0;
59+
overflow: auto;
60+
}
61+
5162
.ch-editor-body code {
5263
line-height: 20px;
5364
}

packages/mini-editor/src/mini-editor.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export type MiniEditorProps = {
3131
steps?: MiniEditorStep[]
3232
height?: number
3333
minColumns?: number
34+
minZoom?: number
35+
maxZoom?: number
3436
button?: React.ReactNode
3537
classes?: Classes
3638
} & React.PropsWithoutRef<JSX.IntrinsicElements["div"]>
@@ -46,6 +48,8 @@ function MiniEditor(props: MiniEditorProps) {
4648
steps: ogSteps,
4749
tabs: ogTabs,
4850
minColumns = 50,
51+
minZoom = 0.2,
52+
maxZoom = 1,
4953
height,
5054
...rest
5155
} = props
@@ -101,6 +105,8 @@ function MiniEditor(props: MiniEditorProps) {
101105
steps={contentSteps}
102106
parentHeight={height}
103107
minColumns={minColumns}
108+
minZoom={minZoom}
109+
maxZoom={maxZoom}
104110
/>
105111
)}
106112
</EditorFrame>
@@ -155,6 +161,8 @@ type ContentProps = {
155161
steps: ContentStep[]
156162
parentHeight?: number
157163
minColumns: number
164+
minZoom: number
165+
maxZoom: number
158166
}
159167

160168
function EditorContent({
@@ -163,6 +171,8 @@ function EditorContent({
163171
steps,
164172
parentHeight,
165173
minColumns,
174+
minZoom,
175+
maxZoom,
166176
}: ContentProps) {
167177
const fwdTransitions = useForwardTransitions(steps)
168178
const bwdTransitions = useBackwardTransitions(steps)
@@ -188,6 +198,8 @@ function EditorContent({
188198
progress={progress - transitionIndex + 1}
189199
parentHeight={parentHeight}
190200
minColumns={minColumns}
201+
minZoom={minZoom}
202+
maxZoom={maxZoom}
191203
/>
192204
)
193205
}

packages/mini-editor/src/use-dimensions.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ function useDimensions<T extends HTMLElement>(
3131

3232
useLayoutEffect(() => {
3333
if (ref.current) {
34-
const rect = ref.current.getBoundingClientRect()
35-
3634
const pll = ref.current.querySelector(
3735
".prev-longest-line"
3836
)
@@ -50,8 +48,8 @@ function useDimensions<T extends HTMLElement>(
5048
? plw! / (pll.textContent?.length || 1)
5149
: nlw! / (nll!.textContent?.length || 1)
5250
setDimensions({
53-
width: rect.width,
54-
height: rect.height,
51+
width: getWidthWithoutPadding(ref.current),
52+
height: getHeightWithoutPadding(ref.current),
5553
lineWidths: [
5654
plw || nlw || DEFAULT_WIDTH,
5755
nlw || plw || DEFAULT_WIDTH,
@@ -73,6 +71,23 @@ function useDimensions<T extends HTMLElement>(
7371
}
7472
}
7573

74+
function getWidthWithoutPadding(element: HTMLElement) {
75+
const computedStyle = getComputedStyle(element)
76+
return (
77+
element.clientWidth -
78+
parseFloat(computedStyle.paddingLeft) -
79+
parseFloat(computedStyle.paddingRight)
80+
)
81+
}
82+
function getHeightWithoutPadding(element: HTMLElement) {
83+
const computedStyle = getComputedStyle(element)
84+
return (
85+
element.clientHeight -
86+
parseFloat(computedStyle.paddingTop) -
87+
parseFloat(computedStyle.paddingBottom)
88+
)
89+
}
90+
7691
function depsChanged(
7792
oldDeps: React.DependencyList,
7893
newDeps: React.DependencyList

packages/smooth-lines/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function Container({
208208
width,
209209
height,
210210
position: "relative",
211-
overflow: "auto",
211+
// overflow: "auto",
212212
}}
213213
>
214214
{children}

packages/storybook/src/mini-editor.story.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ console.log(1)`
6969
progress={progress}
7070
backward={backward}
7171
minColumns={10}
72+
minZoom={1}
7273
/>
7374
)}
7475
</WithProgress>
@@ -127,6 +128,7 @@ console.log(8)`
127128
steps={steps}
128129
progress={progress}
129130
backward={backward}
131+
minZoom={0.8}
130132
/>
131133
)}
132134
</WithProgress>

packages/storybook/src/scrollycoding.story.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ function Hike({
8787
},
8888
{
8989
minColumns: 46,
90+
minZoom: 0.9,
9091
...codeProps,
9192
}
9293
)

0 commit comments

Comments
 (0)