Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add inline widget option to React component

Tim Wilsondd00243241bc
diff --git a/apps/web/ui/layout/sidebar/refer-button.tsx b/apps/web/ui/layout/sidebar/refer-button.tsx
index 0cd6218fe0..a2800599d0 100644
--- a/apps/web/ui/layout/sidebar/refer-button.tsx
+++ b/apps/web/ui/layout/sidebar/refer-button.tsx
@@ -40,7 +40,7 @@ export function ReferButton() {
if (!referralLinkId || !publicToken) return null;
return (
- <DubWidget token={publicToken} trigger="manual">
+ <DubWidget token={publicToken} options={{ trigger: "manual" }}>
<button
type="button"
className={cn(
diff --git a/packages/embeds/react/src/widget.tsx b/packages/embeds/react/src/widget.tsx
index 5c3751093b..71fd9f6385 100644
--- a/packages/embeds/react/src/widget.tsx
+++ b/packages/embeds/react/src/widget.tsx
@@ -5,39 +5,92 @@ import {
Children,
cloneElement,
HTMLAttributes,
+ HTMLProps,
isValidElement,
memo,
PropsWithChildren,
+ ReactNode,
useEffect,
useId,
useRef,
} from "react";
+type Options = Omit<DubOptions, "variant" | "token">;
+
+type DubWidgetProps = {
+ token: DubOptions["token"];
+ options?: Options;
+} & (
+ | ({
+ variant: "inline";
+ } & HTMLProps<HTMLDivElement>)
+ | {
+ variant?: "popup";
+ children?: ReactNode;
+ }
+);
+
export const DubWidget = memo(
- ({ children, ...options }: PropsWithChildren<DubOptions>) => {
- const id = useId();
- const toggleWidgetRef = useRef<() => void>();
-
- useEffect(() => {
- const { destroy, toggleWidget } =
- init({
- id,
- ...options,
- }) || {};
-
- toggleWidgetRef.current = toggleWidget;
-
- return () => destroy?.();
- }, [children, id, options]);
-
- return children ? (
- <Slot id={id} onClick={() => toggleWidgetRef.current?.()}>
- {children}
- </Slot>
- ) : null;
+ ({ variant = "popup", token, options, ...rest }: DubWidgetProps) => {
+ return variant === "inline" ? (
+ <DubInline options={{ ...options, token }} {...rest} />
+ ) : (
+ <DubPopup options={{ ...options, token }} {...rest} />
+ );
},
);
+function DubInline({
+ options,
+ ...rest
+}: { options: Omit<DubOptions, "variant"> } & HTMLProps<HTMLDivElement>) {
+ const id = useId();
+ const rootRef = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ if (!rootRef.current) return;
+
+ const { destroy } =
+ init({
+ id,
+ variant: "inline",
+ root: rootRef.current,
+ ...options,
+ }) || {};
+
+ return () => destroy?.();
+ }, [id, options]);
+
+ return <div {...rest} ref={rootRef} />;
+}
+
+function DubPopup({
+ children,
+ options,
+}: PropsWithChildren<{ options: Omit<DubOptions, "variant"> }>) {
+ const id = useId();
+ const toggleWidgetRef = useRef<() => void>();
+
+ useEffect(() => {
+ const { destroy, toggleWidget } =
+ init({
+ id,
+ variant: "popup",
+ ...options,
+ }) || {};
+
+ toggleWidgetRef.current = toggleWidget;
+
+ return () => destroy?.();
+ }, [children, id, options]);
+
+ return children ? (
+ <Slot id={id} onClick={() => toggleWidgetRef.current?.()}>
+ {children}
+ </Slot>
+ ) : null;
+}
+
function Slot({
children,
...props