Embedded Routing Forms - Part1 (#3530)
* Support dark theme in routing form * Fix Embed detection * Add routing form embed example * Better rendering support in dark mode for react-select * Fix more theme issues * Added test for Routing Form Embed Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
e871781079
commit
1be5510c5e
15 files changed
+447
-135
No files matched your search
@@ -2,6 +2,7 @@ import React, { useCallback, useEffect, useState } from "react";
|
||||
import ReactSelect, { components, GroupBase, Props, InputProps, SingleValue, MultiValue } from "react-select";
|
||||
|
||||
import classNames from "@lib/classNames";
|
||||
import useTheme from "@lib/hooks/useTheme";
|
||||
|
||||
export type SelectProps<
|
||||
Option,
|
||||
@@ -27,6 +28,54 @@ function Select<
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({ className, ...props }: SelectProps<Option, IsMulti, Group>) {
|
||||
const { resolvedTheme, forcedTheme, isReady } = useTheme();
|
||||
const hasDarkTheme = !forcedTheme && resolvedTheme === "dark";
|
||||
const darkThemeColors = {
|
||||
/** Dark Theme starts */
|
||||
//primary - Border when selected and Selected Option background
|
||||
primary: "rgb(41 41 41 / var(--tw-border-opacity))",
|
||||
|
||||
neutral0: "rgb(62 62 62 / var(--tw-bg-opacity))",
|
||||
// Down Arrow hover color
|
||||
neutral5: "white",
|
||||
|
||||
neutral10: "rgb(41 41 41 / var(--tw-border-opacity))",
|
||||
|
||||
// neutral20 - border color + down arrow default color
|
||||
neutral20: "rgb(41 41 41 / var(--tw-border-opacity))",
|
||||
|
||||
// neutral30 - hover border color
|
||||
neutral30: "rgb(41 41 41 / var(--tw-border-opacity))",
|
||||
|
||||
neutral40: "white",
|
||||
|
||||
danger: "white",
|
||||
|
||||
// Cross button in multiselect
|
||||
dangerLight: "rgb(41 41 41 / var(--tw-border-opacity))",
|
||||
|
||||
// neutral50 - MultiSelect - "Select Text" color
|
||||
neutral50: "white",
|
||||
|
||||
// neutral60 - Down Arrow color
|
||||
neutral60: "white",
|
||||
|
||||
neutral70: "red",
|
||||
|
||||
// neutral80 - Selected option
|
||||
neutral80: "white",
|
||||
|
||||
neutral90: "blue",
|
||||
|
||||
primary50: "rgba(209 , 213, 219, var(--tw-bg-opacity))",
|
||||
primary25: "rgba(244, 245, 246, var(--tw-bg-opacity))",
|
||||
/** Dark Theme ends */
|
||||
};
|
||||
|
||||
// Till we know in JS the theme is ready, we can't render react-select as it would render with light theme instead
|
||||
if (!isReady) {
|
||||
return <input type="text" className={className} />;
|
||||
}
|
||||
return (
|
||||
<ReactSelect
|
||||
theme={(theme) => ({
|
||||
@@ -34,10 +83,16 @@ function Select<
|
||||
borderRadius: 2,
|
||||
colors: {
|
||||
...theme.colors,
|
||||
primary: "var(--brand-color)",
|
||||
...(hasDarkTheme
|
||||
? darkThemeColors
|
||||
: {
|
||||
/** Light Theme starts */
|
||||
primary: "var(--brand-color)",
|
||||
|
||||
primary50: "rgba(209 , 213, 219, var(--tw-bg-opacity))",
|
||||
primary25: "rgba(244, 245, 246, var(--tw-bg-opacity))",
|
||||
primary50: "rgba(209 , 213, 219, var(--tw-bg-opacity))",
|
||||
primary25: "rgba(244, 245, 246, var(--tw-bg-opacity))",
|
||||
/** Light Theme Ends */
|
||||
}),
|
||||
},
|
||||
})}
|
||||
styles={{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { SessionProvider } from "next-auth/react";
|
||||
import { appWithTranslation } from "next-i18next";
|
||||
import type { AppProps as NextAppProps, AppProps as NextJsAppProps } from "next/app";
|
||||
import { NextRouter } from "next/router";
|
||||
import { ComponentProps, ReactNode } from "react";
|
||||
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
@@ -15,7 +16,10 @@ const I18nextAdapter = appWithTranslation<NextJsAppProps & { children: React.Rea
|
||||
|
||||
// Workaround for https://github.com/vercel/next.js/issues/8592
|
||||
export type AppProps = Omit<NextAppProps, "Component"> & {
|
||||
Component: NextAppProps["Component"] & { requiresLicense?: boolean; isThemeSupported?: boolean };
|
||||
Component: NextAppProps["Component"] & {
|
||||
requiresLicense?: boolean;
|
||||
isThemeSupported?: boolean | ((arg: { router: NextRouter }) => boolean);
|
||||
};
|
||||
/** Will be defined only is there was an error */
|
||||
err?: Error;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useTheme as useNextTheme } from "next-themes";
|
||||
import { useEffect } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { useEmbedTheme } from "@calcom/embed-core/embed-iframe";
|
||||
import { Maybe } from "@calcom/trpc/server";
|
||||
@@ -7,7 +7,8 @@ import { Maybe } from "@calcom/trpc/server";
|
||||
// makes sure the ui doesn't flash
|
||||
export default function useTheme(theme?: Maybe<string>) {
|
||||
theme = theme || "system";
|
||||
const { theme: currentTheme, setTheme } = useNextTheme();
|
||||
const { resolvedTheme, setTheme, forcedTheme } = useNextTheme();
|
||||
const [isReady, setIsReady] = useState<boolean>(false);
|
||||
const embedTheme = useEmbedTheme();
|
||||
// Embed UI configuration takes more precedence over App Configuration
|
||||
theme = embedTheme || theme;
|
||||
@@ -16,10 +17,13 @@ export default function useTheme(theme?: Maybe<string>) {
|
||||
if (theme) {
|
||||
setTheme(theme);
|
||||
}
|
||||
setIsReady(true);
|
||||
}, [theme, setTheme]);
|
||||
|
||||
return {
|
||||
currentTheme,
|
||||
resolvedTheme,
|
||||
setTheme,
|
||||
isReady,
|
||||
forcedTheme,
|
||||
};
|
||||
}
|
||||
+22
-4
@@ -17,7 +17,6 @@ import LicenseRequired from "@ee/components/LicenseRequired";
|
||||
|
||||
import AppProviders, { AppProps } from "@lib/app-providers";
|
||||
import { seoConfig } from "@lib/config/next-seo.config";
|
||||
import useTheme from "@lib/hooks/useTheme";
|
||||
|
||||
import I18nLanguageHandler from "@components/I18nLanguageHandler";
|
||||
|
||||
@@ -28,13 +27,28 @@ import "../styles/globals.css";
|
||||
function MyApp(props: AppProps) {
|
||||
const { Component, pageProps, err, router } = props;
|
||||
let pageStatus = "200";
|
||||
|
||||
if (router.pathname === "/404") {
|
||||
pageStatus = "404";
|
||||
} else if (router.pathname === "/500") {
|
||||
pageStatus = "500";
|
||||
}
|
||||
const forcedTheme = Component.isThemeSupported ? undefined : "light";
|
||||
|
||||
let isThemeSupported = null;
|
||||
|
||||
if (typeof Component.isThemeSupported === "function") {
|
||||
isThemeSupported = Component.isThemeSupported({ router });
|
||||
} else {
|
||||
isThemeSupported = Component.isThemeSupported;
|
||||
}
|
||||
|
||||
const forcedTheme = isThemeSupported ? undefined : "light";
|
||||
|
||||
// Use namespace of embed to ensure same namespaced embed are displayed with same theme. This allows different embeds on the same website to be themed differently
|
||||
// One such example is our Embeds Demo and Testing page at http://localhost:3100
|
||||
// Having `getEmbedNamespace` defined on window before react initializes the app, ensures that embedNamespace is available on the first mount and can be used as part of storageKey
|
||||
const embedNamespace = typeof window !== "undefined" ? window.getEmbedNamespace() : null;
|
||||
const storageKey = typeof embedNamespace === "string" ? `embed-theme-${embedNamespace}` : "theme";
|
||||
|
||||
return (
|
||||
<EventCollectionProvider options={{ apiPath: "/api/collect-events" }}>
|
||||
<ContractsProvider>
|
||||
@@ -46,7 +60,11 @@ function MyApp(props: AppProps) {
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
|
||||
</Head>
|
||||
{/* color-scheme makes background:transparent not work which is required by embed. We need to ensure next-theme adds color-scheme to `body` instead of `html`(https://github.com/pacocoursey/next-themes/blob/main/src/index.tsx#L74). Once that's done we can enable color-scheme support */}
|
||||
<ThemeProvider enableColorScheme={false} forcedTheme={forcedTheme} attribute="class">
|
||||
<ThemeProvider
|
||||
enableColorScheme={false}
|
||||
storageKey={storageKey}
|
||||
forcedTheme={forcedTheme}
|
||||
attribute="class">
|
||||
{Component.requiresLicense ? (
|
||||
<LicenseRequired>
|
||||
<Component {...pageProps} err={err} />
|
||||
|
||||
@@ -2,6 +2,24 @@ import Document, { DocumentContext, Head, Html, Main, NextScript, DocumentProps
|
||||
|
||||
type Props = Record<string, unknown> & DocumentProps;
|
||||
|
||||
function toRunBeforeReactOnClient() {
|
||||
window.sessionStorage.setItem("calEmbedMode", String(location.search.includes("embed=")));
|
||||
window.isEmbed = () => {
|
||||
return window.sessionStorage.getItem("calEmbedMode") === "true";
|
||||
};
|
||||
|
||||
window.getEmbedTheme = () => {
|
||||
const url = new URL(document.URL);
|
||||
return url.searchParams.get("theme") as "dark" | "light";
|
||||
};
|
||||
|
||||
window.getEmbedNamespace = () => {
|
||||
const url = new URL(document.URL);
|
||||
const namespace = url.searchParams.get("embed");
|
||||
return namespace;
|
||||
};
|
||||
}
|
||||
|
||||
class MyDocument extends Document<Props> {
|
||||
static async getInitialProps(ctx: DocumentContext) {
|
||||
const initialProps = await Document.getInitialProps(ctx);
|
||||
@@ -31,13 +49,12 @@ class MyDocument extends Document<Props> {
|
||||
crossOrigin="anonymous"
|
||||
/>
|
||||
<link rel="preload" href="/fonts/cal.ttf" as="font" type="font/ttf" crossOrigin="anonymous" />
|
||||
{/* Define isEmbed here so that it can be shared with App(embed-iframe) as well as the following code to change background and hide body */}
|
||||
{/* Define isEmbed here so that it can be shared with App(embed-iframe) as well as the following code to change background and hide body
|
||||
Persist the embed mode in sessionStorage because query param might get lost during browsing.
|
||||
*/}
|
||||
<script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `
|
||||
window.isEmbed = ()=> {
|
||||
return location.search.includes("embed=")
|
||||
}`,
|
||||
__html: `(${toRunBeforeReactOnClient.toString()})()`,
|
||||
}}
|
||||
/>
|
||||
</Head>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { GetServerSidePropsContext } from "next";
|
||||
import { useRouter } from "next/router";
|
||||
import { NextRouter, useRouter } from "next/router";
|
||||
|
||||
import RoutingFormsRoutingConfig from "@calcom/app-store/ee/routing_forms/pages/app-routing.config";
|
||||
import prisma from "@calcom/prisma";
|
||||
@@ -31,7 +31,7 @@ function getRoute(appName: string, pages: string[]) {
|
||||
notFound: false;
|
||||
// A component than can accept any properties
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
Component: (props: any) => JSX.Element;
|
||||
Component: ((props: any) => JSX.Element) & { isThemeSupported?: boolean };
|
||||
getServerSideProps: AppGetServerSideProps;
|
||||
};
|
||||
if (!appPage) {
|
||||
@@ -59,7 +59,13 @@ export default function AppPage(props: inferSSRProps<typeof getServerSideProps>)
|
||||
return <route.Component {...componentProps} />;
|
||||
}
|
||||
|
||||
AppPage.isThemeSupported = true;
|
||||
AppPage.isThemeSupported = ({ router }: { router: NextRouter }) => {
|
||||
const route = getRoute(router.query.slug as string, router.query.pages as string[]);
|
||||
if (route.notFound) {
|
||||
return false;
|
||||
}
|
||||
return route.Component.isThemeSupported;
|
||||
};
|
||||
|
||||
export async function getServerSideProps(
|
||||
context: GetServerSidePropsContext<{
|
||||
|
||||
Reference in new issue
Block a user