Skip to content

ref(feedback): Simplify feedback function params #11957

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

Merged
merged 1 commit into from
May 8, 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
43 changes: 8 additions & 35 deletions packages/feedback/src/core/createMainStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,26 @@ function getThemedCssVariables(theme: FeedbackInternalOptions['themeLight']): st
--border: ${theme.border};
--border-radius: ${theme.borderRadius};
--box-shadow: ${theme.boxShadow};

--submit-background: ${theme.submitBackground};
--submit-background-hover: ${theme.submitBackgroundHover};
--submit-border: ${theme.submitBorder};
--submit-outline-focus: ${theme.submitOutlineFocus};
--submit-foreground: ${theme.submitForeground};
--submit-foreground-hover: ${theme.submitForegroundHover};

--cancel-background: ${theme.cancelBackground};
--cancel-background-hover: ${theme.cancelBackgroundHover};
--cancel-border: ${theme.cancelBorder};
--cancel-outline-focus: ${theme.cancelOutlineFocus};
--cancel-foreground: ${theme.cancelForeground};
--cancel-foreground-hover: ${theme.cancelForegroundHover};

--input-background: ${theme.inputBackground};
--input-foreground: ${theme.inputForeground};
--input-border: ${theme.inputBorder};
--input-outline-focus: ${theme.inputOutlineFocus};

--form-border-radius: ${theme.formBorderRadius};
--form-content-border-radius: ${theme.formContentBorderRadius};
`;
}

/**
* Creates <style> element for widget actor (button that opens the dialog)
*/
export function createMainStyles(
colorScheme: 'system' | 'dark' | 'light',
themes: Pick<FeedbackInternalOptions, 'themeLight' | 'themeDark'>,
): HTMLStyleElement {
export function createMainStyles({ colorScheme, themeDark, themeLight }: FeedbackInternalOptions): HTMLStyleElement {
const style = DOCUMENT.createElement('style');
style.textContent = `
:host {
--z-index: ${themes.themeLight.zIndex};
--font-family: ${themes.themeLight.fontFamily};
--font-size: ${themes.themeLight.fontSize};
--z-index: ${themeLight.zIndex};
--font-family: ${themeLight.fontFamily};
--font-size: ${themeLight.fontSize};

font-family: var(--font-family);
font-size: var(--font-size);

--page-margin: 16px;
--actor-inset: auto var(--page-margin) var(--page-margin) auto;

--dialog-inset: auto var(--page-margin) var(--page-margin) auto;
--dialog-padding: 24px;

Comment on lines -59 to -61
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these and a bunch of stuff above are in Dialog.css.ts now, along with all the :host { ... } stuff to wrap it.

.brand-link path {
fill: ${colorScheme === 'dark' ? '#fff' : '#362d59'};
}
Expand All @@ -69,20 +41,21 @@ export function createMainStyles(
}
}

${getThemedCssVariables(colorScheme === 'dark' ? themes.themeDark : themes.themeLight)}
${getThemedCssVariables(colorScheme === 'dark' ? themeDark : themeLight)}
}

${
colorScheme === 'system'
? `
@media (prefers-color-scheme: dark) {
:host {
${getThemedCssVariables(themes.themeDark)}
${getThemedCssVariables(themeDark)}
}
}`
: ''
}
}`;
}
`;

return style;
}
4 changes: 2 additions & 2 deletions packages/feedback/src/core/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export const buildFeedbackIntegration = ({

// FeedbackTextConfiguration
addScreenshotButtonLabel = ADD_SCREENSHOT_LABEL,
triggerLabel = TRIGGER_LABEL,
cancelButtonLabel = CANCEL_BUTTON_LABEL,
confirmButtonLabel = CONFIRM_BUTTON_LABEL,
emailLabel = EMAIL_LABEL,
Expand All @@ -98,6 +97,7 @@ export const buildFeedbackIntegration = ({
removeScreenshotButtonLabel = REMOVE_SCREENSHOT_LABEL,
submitButtonLabel = SUBMIT_BUTTON_LABEL,
successMessageText = SUCCESS_MESSAGE_TEXT,
triggerLabel = TRIGGER_LABEL,

// FeedbackCallbacks
onFormOpen,
Expand Down Expand Up @@ -163,7 +163,7 @@ export const buildFeedbackIntegration = ({
DOCUMENT.body.appendChild(host);

_shadow = host.attachShadow({ mode: 'open' });
_shadow.appendChild(createMainStyles(options.colorScheme, options));
_shadow.appendChild(createMainStyles(options));
}
return _shadow as ShadowRoot;
};
Expand Down
60 changes: 53 additions & 7 deletions packages/feedback/src/modal/components/Dialog.css.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FeedbackInternalOptions } from '@sentry/types';
import { DOCUMENT } from '../../constants';

const DIALOG = `
Expand Down Expand Up @@ -237,19 +238,64 @@ const SUCCESS = `
}
`;

function getThemedCssVariables(theme: FeedbackInternalOptions['themeLight']): string {
return `
--submit-background: ${theme.submitBackground};
--submit-background-hover: ${theme.submitBackgroundHover};
--submit-border: ${theme.submitBorder};
--submit-outline-focus: ${theme.submitOutlineFocus};
--submit-foreground: ${theme.submitForeground};
--submit-foreground-hover: ${theme.submitForegroundHover};

--cancel-background: ${theme.cancelBackground};
--cancel-background-hover: ${theme.cancelBackgroundHover};
--cancel-border: ${theme.cancelBorder};
--cancel-outline-focus: ${theme.cancelOutlineFocus};
--cancel-foreground: ${theme.cancelForeground};
--cancel-foreground-hover: ${theme.cancelForegroundHover};

--input-background: ${theme.inputBackground};
--input-foreground: ${theme.inputForeground};
--input-border: ${theme.inputBorder};
--input-outline-focus: ${theme.inputOutlineFocus};

--form-border-radius: ${theme.formBorderRadius};
--form-content-border-radius: ${theme.formContentBorderRadius};
`;
}

/**
* Creates <style> element for widget dialog
*/
export function createDialogStyles(): HTMLStyleElement {
export function createDialogStyles({ colorScheme, themeDark, themeLight }: FeedbackInternalOptions): HTMLStyleElement {
const style = DOCUMENT.createElement('style');

style.textContent = `
${DIALOG}
${DIALOG_HEADER}
${FORM}
${BUTTON}
${SUCCESS}
`;
:host {
--dialog-inset: auto var(--page-margin) var(--page-margin) auto;
--dialog-padding: 24px;

${getThemedCssVariables(colorScheme === 'dark' ? themeDark : themeLight)}
}

${
colorScheme === 'system'
? `
@media (prefers-color-scheme: dark) {
:host {
${getThemedCssVariables(themeDark)}
}
}`
: ''
}
}

${DIALOG}
${DIALOG_HEADER}
${FORM}
${BUTTON}
${SUCCESS}
`;

return style;
}
9 changes: 5 additions & 4 deletions packages/feedback/src/modal/components/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FeedbackFormData } from '@sentry/types';
import type { FeedbackFormData, FeedbackInternalOptions } from '@sentry/types';
// biome-ignore lint/nursery/noUnusedImports: reason
import { Fragment, h } from 'preact'; // eslint-disable-line @typescript-eslint/no-unused-vars
import type { VNode } from 'preact';
Expand All @@ -11,12 +11,13 @@ import { Form } from './Form';
import { SuccessIcon } from './SuccessIcon';

interface Props extends HeaderProps, FormProps {
successMessageText: string;
onFormSubmitted: () => void;
open: boolean;
options: FeedbackInternalOptions;
}

export function Dialog({ open, onFormSubmitted, successMessageText, ...props }: Props): VNode {
export function Dialog({ open, onFormSubmitted, ...props }: Props): VNode {
const options = props.options;
const successIconHtml = useMemo(() => ({ __html: SuccessIcon().outerHTML }), []);

const [timeoutId, setTimeoutId] = useState<NodeJS.Timeout | null>(null);
Expand Down Expand Up @@ -46,7 +47,7 @@ export function Dialog({ open, onFormSubmitted, successMessageText, ...props }:
<Fragment>
{timeoutId ? (
<div class="success-message" onClick={handleOnSuccessClick}>
{successMessageText}
{options.successMessageText}
<span class="success-icon" dangerouslySetInnerHTML={successIconHtml} />
</div>
) : (
Expand Down
9 changes: 4 additions & 5 deletions packages/feedback/src/modal/components/DialogHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import { useMemo } from 'preact/hooks';
import { SentryLogo } from './SentryLogo';

export interface Props {
formTitle: FeedbackInternalOptions['formTitle'];
showBranding: FeedbackInternalOptions['showBranding'];
options: FeedbackInternalOptions;
}

export function DialogHeader({ formTitle, showBranding }: Props): VNode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: i kind of like having the specific props, esp for this component where it only needs 2 of the options

export function DialogHeader({ options }: Props): VNode {
const logoHtml = useMemo(() => ({ __html: SentryLogo().outerHTML }), []);

return (
<h2 class="dialog__header">
{formTitle}
{showBranding ? (
{options.formTitle}
{options.showBranding ? (
<a
class="brand-link"
target="_blank"
Expand Down
51 changes: 19 additions & 32 deletions packages/feedback/src/modal/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,8 @@ import { FEEDBACK_WIDGET_SOURCE } from '../../constants';
import { DEBUG_BUILD } from '../../util/debug-build';
import { getMissingFields } from '../../util/validate';

export interface Props
extends Pick<
FeedbackInternalOptions,
| 'addScreenshotButtonLabel'
| 'removeScreenshotButtonLabel'
| 'cancelButtonLabel'
| 'emailLabel'
| 'emailPlaceholder'
| 'isEmailRequired'
| 'isNameRequired'
| 'messageLabel'
| 'messagePlaceholder'
| 'nameLabel'
| 'namePlaceholder'
| 'showEmail'
| 'showName'
| 'submitButtonLabel'
| 'isRequiredLabel'
> {
export interface Props extends Pick<FeedbackInternalOptions, 'showEmail' | 'showName'> {
options: FeedbackInternalOptions;
defaultEmail: string;
defaultName: string;
onFormClose: () => void;
Expand All @@ -50,29 +33,33 @@ function retrieveStringValue(formData: FormData, key: string): string {
}

export function Form({
addScreenshotButtonLabel,
removeScreenshotButtonLabel,
cancelButtonLabel,
options,
defaultEmail,
defaultName,
emailLabel,
emailPlaceholder,
isEmailRequired,
isNameRequired,
messageLabel,
messagePlaceholder,
nameLabel,
namePlaceholder,

onFormClose,
onSubmit,
onSubmitSuccess,
onSubmitError,
showEmail,
showName,
submitButtonLabel,
isRequiredLabel,
screenshotInput,
}: Props): VNode {
const {
addScreenshotButtonLabel,
removeScreenshotButtonLabel,
cancelButtonLabel,
emailLabel,
emailPlaceholder,
isEmailRequired,
isNameRequired,
messageLabel,
messagePlaceholder,
nameLabel,
namePlaceholder,
submitButtonLabel,
isRequiredLabel,
} = options;
// TODO: set a ref on the form, and whenever an input changes call proceessForm() and setError()
const [error, setError] = useState<null | string>(null);

Expand Down
19 changes: 2 additions & 17 deletions packages/feedback/src/modal/integration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const feedbackModalIntegration = ((): FeedbackModalIntegration => {
const user = getCurrentScope().getUser() || getIsolationScope().getUser() || getGlobalScope().getUser();

const el = DOCUMENT.createElement('div');
const style = createDialogStyles();
const style = createDialogStyles(options);

let originalOverflow = '';
const dialog = {
Expand Down Expand Up @@ -50,27 +50,12 @@ export const feedbackModalIntegration = ((): FeedbackModalIntegration => {
const renderContent = (open: boolean): void => {
render(
<Dialog
options={options}
screenshotInput={screenshotInput}
showBranding={options.showBranding}
showName={options.showName || options.isNameRequired}
showEmail={options.showEmail || options.isEmailRequired}
isNameRequired={options.isNameRequired}
isEmailRequired={options.isEmailRequired}
formTitle={options.formTitle}
cancelButtonLabel={options.cancelButtonLabel}
submitButtonLabel={options.submitButtonLabel}
emailLabel={options.emailLabel}
emailPlaceholder={options.emailPlaceholder}
messageLabel={options.messageLabel}
messagePlaceholder={options.messagePlaceholder}
nameLabel={options.nameLabel}
namePlaceholder={options.namePlaceholder}
defaultName={(userKey && user && user[userKey.name]) || ''}
defaultEmail={(userKey && user && user[userKey.email]) || ''}
successMessageText={options.successMessageText}
isRequiredLabel={options.isRequiredLabel}
addScreenshotButtonLabel={options.addScreenshotButtonLabel}
removeScreenshotButtonLabel={options.removeScreenshotButtonLabel}
onFormClose={() => {
renderContent(false);
options.onFormClose && options.onFormClose();
Expand Down
Loading