Skip to content

Commit 5da22c3

Browse files
committed
Bound container window position (#9)
Signed-off-by: Jay Wang <[email protected]>
1 parent ad72ee2 commit 5da22c3

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/stickyland.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ import { MyIcons } from './icons';
88

99
const MIN_WIDTH = 235;
1010
const MIN_HEIGHT = 240;
11+
const WINDOW_GAP = 29;
12+
13+
type ContainerPos = {
14+
width: number;
15+
height: number;
16+
y: number;
17+
};
1118

1219
export class StickyLand {
1320
node: HTMLElement;
1421
header: HTMLElement;
1522
stickyTab: StickyTab;
1623
stickyContent: StickyContent | null = null;
1724
floatingWindows: FloatingWindow[] = [];
25+
containerSize: ContainerPos;
1826

1927
constructor(panel: NotebookPanel) {
2028
this.node = document.createElement('div');
@@ -30,6 +38,23 @@ export class StickyLand {
3038
this.header.classList.add('sticky-header');
3139
this.node.appendChild(this.header);
3240

41+
// Bound the window position inside its parent when dragging the header
42+
if (this.node.parentElement) {
43+
const containerBBox = this.node.parentElement.getBoundingClientRect();
44+
this.containerSize = {
45+
width: containerBBox.width,
46+
height: containerBBox.height,
47+
y: containerBBox.y
48+
};
49+
} else {
50+
console.warn('Could not find stickyland container parent.');
51+
this.containerSize = {
52+
width: 2000,
53+
height: 1500,
54+
y: 0
55+
};
56+
}
57+
3358
this.initHeader();
3459

3560
// Add the tab element
@@ -166,10 +191,14 @@ export class StickyLand {
166191
e.preventDefault();
167192
e.stopPropagation();
168193
const mouseEvent = e as MouseEvent;
169-
170194
const newTop = mouseEvent.pageY + yOffset;
171195

172-
this.node.style.top = `${newTop}px`;
196+
const nodeBBox = this.node.getBoundingClientRect();
197+
const maxNewY = this.containerSize.height - nodeBBox.height;
198+
let newY = Math.max(WINDOW_GAP, newTop);
199+
newY = Math.min(maxNewY, newY);
200+
201+
this.node.style.top = `${newY}px`;
173202
};
174203

175204
const mouseUpHandler = () => {

style/base.css

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
21
.sticky-button.jp-Button.minimal .jp-Icon {
32
color: var(--jp-inverse-layout-color3);
43
}
54

65
.sticky-container {
76
position: absolute;
8-
z-index: 5;
7+
z-index: 7;
98
right: 0px;
109
opacity: 0.97;
1110

1211
border-top-left-radius: 5px;
1312
border-bottom-left-radius: 5px;
1413

1514
box-shadow: -1px 1px 3px hsla(0, 0%, 0%, 0.15),
16-
-1px 1px 20px hsla(0, 0%, 0%, 0.1);
15+
-1px 1px 20px hsla(0, 0%, 0%, 0.1);
1716
width: 300px;
1817
height: 300px;
1918

@@ -48,7 +47,7 @@
4847
padding: 0px 5px;
4948
text-align: center;
5049
white-space: nowrap;
51-
border-radius: .375em;
50+
border-radius: 0.375em;
5251
box-shadow: none;
5352

5453
display: inline-flex;
@@ -87,7 +86,7 @@
8786
text-align: left;
8887
min-width: 40px;
8988

90-
border-top: 2px solid #1976D2;
89+
border-top: 2px solid #1976d2;
9190
border-radius: 0;
9291

9392
position: relative;
@@ -111,7 +110,7 @@
111110
min-height: 20px;
112111
}
113112

114-
.sticky-tab button.add-tab svg{
113+
.sticky-tab button.add-tab svg {
115114
fill: var(--jp-inverse-layout-color3);
116115
color: var(--jp-inverse-layout-color3);
117116
display: block;
@@ -299,4 +298,4 @@
299298

300299
.no-display {
301300
display: none;
302-
}
301+
}

0 commit comments

Comments
 (0)