Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add theme option #247

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to this project will be documented in this file.

## [2.1.0] - 2024-03-14
- Add `theme` parameter

## [2.0.0] - 2024-01-25
- [BREAKING CHANGE] Rename `onrampToken` parameter to `sessionToken`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coinbase/cbpay-js",
"version": "2.0.0",
"version": "2.1.0",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
2 changes: 2 additions & 0 deletions src/onramp/generateOnRampURL.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEFAULT_HOST } from '../config';
import { OnRampAppParams } from '../types/onramp';
import type { Theme } from '../types/widget';

export type GenerateOnRampURLOptions = {
/** This & destinationWallets or sessionToken are required. */
Expand All @@ -8,6 +9,7 @@ export type GenerateOnRampURLOptions = {
host?: string;
/** This or appId & destinationWallets are required. */
sessionToken?: string;
theme?: Theme;
} & Omit<OnRampAppParams, 'destinationWallets'>;

export const generateOnRampURL = ({
Expand Down
1 change: 1 addition & 0 deletions src/onramp/initOnRamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const initOnRamp = (
const url = generateOnRampURL({
appId: options.appId,
host: options.host,
theme: options.theme,
...widgetParameters,
});
window.open(url);
Expand Down
3 changes: 3 additions & 0 deletions src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type IntegrationType = 'direct' | 'secure_standalone';

export type Experience = 'embedded' | 'popup' | 'new_tab';

export type Theme = 'light' | 'dark';

export type EmbeddedContentStyles = {
target?: string;
width?: string;
Expand All @@ -20,6 +22,7 @@ export type CBPayExperienceOptions<T> = {
appId: string;
host?: string;
debug?: boolean;
theme?: Theme;
onExit?: (error?: Error) => void;
onSuccess?: () => void;
onEvent?: (event: EventMetadata) => void;
Expand Down
10 changes: 9 additions & 1 deletion src/utils/CoinbasePixel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DEFAULT_HOST } from '../config';
import { EmbeddedContentStyles, Experience } from 'types/widget';
import { EmbeddedContentStyles, Experience, Theme } from 'types/widget';
import { createEmbeddedContent, EMBEDDED_IFRAME_ID } from './createEmbeddedContent';
import { JsonObject } from 'types/JsonTypes';
import { broadcastPostMessage, onBroadcastedPostMessage } from './postMessage';
Expand Down Expand Up @@ -37,6 +37,7 @@ export type CoinbasePixelConstructorParams = {
/** Fallback open callback when the pixel failed to load */
onFallbackOpen?: () => void;
debug?: boolean;
theme?: Theme;
};

export type OpenExperienceOptions = {
Expand Down Expand Up @@ -69,6 +70,7 @@ export class CoinbasePixel {
/** onReady callback which should be triggered when a nonce has successfully been retrieved */
private onReadyCallback: CoinbasePixelConstructorParams['onReady'];
private onFallbackOpen: CoinbasePixelConstructorParams['onFallbackOpen'];
private theme: Theme | null | undefined;

public isLoggedIn = false;

Expand All @@ -79,13 +81,15 @@ export class CoinbasePixel {
onReady,
onFallbackOpen,
debug,
theme,
}: CoinbasePixelConstructorParams) {
this.host = host;
this.appId = appId;
this.appParams = appParams;
this.onReadyCallback = onReady;
this.onFallbackOpen = onFallbackOpen;
this.debug = debug || false;
this.theme = theme;

this.addPixelReadyListener();
this.addErrorListener();
Expand Down Expand Up @@ -134,6 +138,10 @@ export class CoinbasePixel {
widgetUrl.searchParams.append('appId', this.appId);
widgetUrl.searchParams.append('type', 'secure_standalone');

if (this.theme) {
widgetUrl.searchParams.append('theme', this.theme);
}

const experience = this.isLoggedIn
? experienceLoggedIn
: experienceLoggedOut || experienceLoggedIn;
Expand Down
Loading