Skip to content

Commit 7238189

Browse files
author
Austin Fay
authored
feat: window and toast features (#94)
* feat: window and toast features * refactor: showToast param names; open/closeWindow * chore: version bump
1 parent feae41b commit 7238189

File tree

5 files changed

+95
-33
lines changed

5 files changed

+95
-33
lines changed

npm/sdk/examples/react-basic/package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

npm/sdk/examples/react-basic/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"preview": "vite preview"
1010
},
1111
"dependencies": {
12-
"@trufflehq/sdk": "^0.2.2",
12+
"@trufflehq/sdk": "^0.3.0",
1313
"react": "^18.2.0",
1414
"react-dom": "^18.2.0"
1515
},

npm/sdk/examples/react-basic/src/App.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useState } from 'react';
2-
import reactLogo from './assets/react.svg';
3-
import './App.css';
4-
import { getEmbed } from '@trufflehq/sdk';
1+
import { useState } from "react";
2+
import reactLogo from "./assets/react.svg";
3+
import "./App.css";
4+
import { getEmbed } from "@trufflehq/sdk";
55

66
const embed = getEmbed();
77

@@ -13,30 +13,41 @@ function App() {
1313

1414
const setSize = () => {
1515
if (isSmall) {
16-
embed.setSize('800px', '800px');
16+
embed.setSize("800px", "800px");
1717
setIsSmall(false);
1818
} else {
19-
embed.setSize('600px', '600px');
19+
embed.setSize("600px", "600px");
2020
setIsSmall(true);
2121
}
2222
};
2323

2424
const setBorder = () => {
2525
if (hasBorder) {
2626
embed.setStyles({
27-
border: 'none',
27+
border: "none",
2828
});
2929
setHasBorder(false);
3030
} else {
3131
embed.setStyles({
32-
border: '5px solid red',
32+
border: "5px solid red",
3333
});
3434
setHasBorder(true);
3535
}
3636
};
3737

3838
const setContainer = () => {
39-
embed.setContainer('#title.ytd-watch-metadata', 'prepend');
39+
embed.setContainer("#title.ytd-watch-metadata", "prepend");
40+
};
41+
42+
const hideWindow = () => {
43+
embed.setWindowVisibility(false);
44+
};
45+
46+
const showToast = () => {
47+
embed.showToast({
48+
message: "Hello World!",
49+
header: "A message from Truffle",
50+
});
4051
};
4152

4253
return (
@@ -66,6 +77,8 @@ function App() {
6677
<button onClick={setSize}>Toggle Size</button>
6778
<button onClick={setBorder}>Toggle Border</button>
6879
<button onClick={setContainer}>Set Container</button>
80+
<button onClick={hideWindow}>Hide Window</button>
81+
<button onClick={showToast}>Show Toast</button>
6982
</div>
7083
);
7184
}

npm/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@trufflehq/sdk",
3-
"version": "0.2.3",
3+
"version": "0.3.1",
44
"description": "The official javascript SDK for Truffle",
55
"keywords": [
66
"truffle",

npm/sdk/src/embed/embed.ts

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { TransframeConsumer } from '@trufflehq/transframe';
2-
import { EmbedSourceApi, getEmbedConsumer } from '../transframe/embed-consumer';
1+
import { TransframeConsumer } from "@trufflehq/transframe";
2+
import { EmbedSourceApi, getEmbedConsumer } from "../transframe/embed-consumer";
33

44
type Styles = Record<string, unknown>;
55

@@ -12,7 +12,7 @@ export class Embed {
1212
}
1313

1414
private _setIframeStyles(styles: Styles) {
15-
this._embedConsumer.call('embedSetStyles', styles);
15+
this._embedConsumer.call("embedSetStyles", styles);
1616
this._currentStyles = styles;
1717
}
1818

@@ -58,7 +58,7 @@ export class Embed {
5858
*/
5959
public setVisibility(isVisible: boolean) {
6060
this.setStyles({
61-
display: isVisible ? 'block' : 'none',
61+
display: isVisible ? "block" : "none",
6262
});
6363
}
6464

@@ -67,12 +67,12 @@ export class Embed {
6767
*/
6868
public setContainer(
6969
querySelector: string,
70-
insertionMethod?: 'append' | 'prepend'
70+
insertionMethod?: "append" | "prepend",
7171
) {
7272
this._embedConsumer.call(
73-
'embedSetContainer',
73+
"embedSetContainer",
7474
querySelector,
75-
insertionMethod
75+
insertionMethod,
7676
);
7777
}
7878

@@ -89,4 +89,53 @@ export class Embed {
8989
public show() {
9090
this.setVisibility(true);
9191
}
92+
93+
/**
94+
* Set the visibility of the window.
95+
*/
96+
public setWindowVisibility(isVisible: boolean) {
97+
this._embedConsumer.call("embedWindowSetVisibility", isVisible);
98+
}
99+
100+
/**
101+
* Get the visibility state of the window.
102+
*/
103+
public async getWindowVisibility() {
104+
return await this._embedConsumer.call("embedWindowGetVisibility");
105+
}
106+
107+
/**
108+
* Open the window.
109+
*/
110+
public openWindow() {
111+
this.setWindowVisibility(true);
112+
}
113+
114+
/**
115+
* Close the window.
116+
*/
117+
public closeWindow() {
118+
this.setWindowVisibility(false);
119+
}
120+
121+
/**
122+
* Shows a toast message.
123+
*/
124+
public showToast(options: {
125+
body: string;
126+
title?: string;
127+
iconUrl?: string;
128+
onClick?: () => void;
129+
}) {
130+
return this._embedConsumer.call(
131+
"embedShowToast",
132+
{
133+
body: options.body,
134+
title: options.title,
135+
iconUrl: options.iconUrl,
136+
},
137+
// transframe callbacks only work if they're passed as top level arguments to a function
138+
options.onClick,
139+
);
140+
}
92141
}

0 commit comments

Comments
 (0)