Skip to content

Commit a79cf5a

Browse files
committed
Add popup component stories
1 parent a09c379 commit a79cf5a

File tree

5 files changed

+83
-27
lines changed

5 files changed

+83
-27
lines changed

src/popup/__tests__/__snapshots__/test_popup.tsx.snap

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
exports[`component renders 1`] = `
44
<div
5-
className="popup-container"
5+
className="popupContainer"
66
>
77
<div
8-
className="container mx-4 my-4"
8+
className="mx-4 my-4"
99
>
1010
<div
1111
className="row"
@@ -22,24 +22,22 @@ exports[`component renders 1`] = `
2222
</div>
2323
<hr />
2424
<div
25-
className="row"
25+
className="grid gap-3 grid-cols-2 mt-3"
2626
>
27-
<div
28-
className="col-lg-12"
27+
<button
28+
className="btn"
29+
data-testid="scroll-to-top"
30+
onClick={[Function]}
2931
>
30-
<button
31-
className="btn btn-block btn-outline-dark"
32-
onClick={[Function]}
33-
>
34-
Scroll To Top
35-
</button>
36-
<button
37-
className="btn btn-block btn-outline-dark"
38-
onClick={[Function]}
39-
>
40-
Scroll To Bottom
41-
</button>
42-
</div>
32+
Scroll To Top
33+
</button>
34+
<button
35+
className="btn"
36+
data-testid="scroll-to-bottom"
37+
onClick={[Function]}
38+
>
39+
Scroll To Bottom
40+
</button>
4341
</div>
4442
</div>
4543
</div>

src/popup/component.tsx

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,66 @@
1-
import React, { FunctionComponent } from "react";
1+
import React from "react";
22
import { Hello } from "@src/components/hello";
3-
import { browser } from "webextension-polyfill-ts";
3+
import { browser, Tabs } from "webextension-polyfill-ts";
44
import { Scroller } from "@src/components/scroller";
5-
import "./styles.scss";
5+
import css from "./styles.module.css";
66

77
// // // //
88

9-
export const Popup: FunctionComponent = () => {
9+
// Scripts to execute in current tab
10+
const scrollToTopScript = `window.scroll(0,0)`;
11+
const scrollToBottomScript = `window.scroll(0,9999999)`;
12+
13+
/**
14+
* Executes a string of Javascript on the current tab
15+
* @param code The string of code to execute on the current tab
16+
*/
17+
function executeScript(code: string): void {
18+
// Query for the active tab in the current window
19+
browser.tabs
20+
.query({ active: true, currentWindow: true })
21+
.then((tabs: Tabs.Tab[]) => {
22+
// Pulls current tab from browser.tabs.query response
23+
const currentTab: Tabs.Tab | undefined = tabs[0];
24+
25+
// Short circuits function execution is current tab isn't found
26+
if (!currentTab) {
27+
return;
28+
}
29+
30+
// Executes the script in the current tab
31+
browser.tabs
32+
.executeScript(currentTab.id, {
33+
code,
34+
})
35+
.then(() => {
36+
console.log("Done Scrolling");
37+
});
38+
});
39+
}
40+
41+
// // // //
42+
43+
export function Popup() {
1044
// Sends the `popupMounted` event
1145
React.useEffect(() => {
1246
browser.runtime.sendMessage({ popupMounted: true });
1347
}, []);
1448

1549
// Renders the component tree
1650
return (
17-
<div className="popup-container">
18-
<div className="container mx-4 my-4">
51+
<div className={css.popupContainer}>
52+
<div className="mx-4 my-4">
1953
<Hello />
2054
<hr />
21-
<Scroller />
55+
<Scroller
56+
onClickScrollTop={() => {
57+
executeScript(scrollToTopScript);
58+
}}
59+
onClickScrollBottom={() => {
60+
executeScript(scrollToBottomScript);
61+
}}
62+
/>
2263
</div>
2364
</div>
2465
);
25-
};
66+
}

src/popup/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from "react";
22
import * as ReactDOM from "react-dom";
33
import { browser } from "webextension-polyfill-ts";
44
import { Popup } from "./component";
5-
import "../scss/app.scss";
5+
import "../css/app.css";
66

77
// // // //
88

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from "react";
2+
import { Popup } from "../component";
3+
import { ComponentMeta } from "@storybook/react";
4+
5+
// // // //
6+
7+
export default {
8+
title: "Components/Popup",
9+
component: Popup,
10+
} as ComponentMeta<typeof Popup>;
11+
12+
export const Render = () => <Popup />;

src/popup/styles.module.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.popupContainer {
2+
min-width: 300px;
3+
min-height: 300px;
4+
display: flex;
5+
}

0 commit comments

Comments
 (0)