Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: center text on OIDC buttons and debounce state update #366

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/elements-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"react-hook-form": "7.53.0",
"react-intl": "6.7.0",
"tailwind-merge": "2.5.2",
"usehooks-ts": "3.1.1",
"zustand": "5.0.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
uiTextToFormattedMessage,
useOryFlow,
} from "@ory/elements-react"
import { ElementType, useEffect } from "react"
import { useFormContext } from "react-hook-form"
import { useIntl } from "react-intl"
import { useDebounceValue } from "usehooks-ts"
import defaultLogos from "../../provider-logos"
import { cn } from "../../utils/cn"
import { useIntl } from "react-intl"
import { ElementType, useEffect, useState } from "react"
import { useFormContext } from "react-hook-form"
import { Spinner } from "./spinner"

export function extractProvider(
Expand Down Expand Up @@ -50,7 +51,9 @@
const {
flow: { ui },
} = useOryFlow()
const [clicked, setClicked] = useState(false)
// Safari cancels form submission events, if we do a state update in the same tick
// so we delay the state update by 100ms
const [clicked, setClicked] = useDebounceValue(false, 100)

Check warning on line 56 in packages/elements-react/src/theme/default/components/form/social.tsx

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/theme/default/components/form/social.tsx#L56

Added line #L56 was not covered by tests
const intl = useIntl()
const {
formState: { isSubmitting },
Expand All @@ -72,15 +75,15 @@
const provider = extractProvider(node.meta.label?.context) ?? ""

const localOnClick = () => {
setClicked(true)
onClick?.()
setClicked(true)

Check warning on line 79 in packages/elements-react/src/theme/default/components/form/social.tsx

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/theme/default/components/form/social.tsx#L79

Added line #L79 was not covered by tests
}

useEffect(() => {
if (!isSubmitting) {
setClicked(false)
}
}, [isSubmitting])
}, [isSubmitting, setClicked])

return (
<button
Expand Down Expand Up @@ -108,9 +111,12 @@
)}
</span>
{showLabel && node.meta.label ? (
<span className="grow text-left font-medium leading-none text-button-social-foreground-default">
{uiTextToFormattedMessage(node.meta.label, intl)}
</span>
<>
<span className="grow text-center font-medium leading-none text-button-social-foreground-default">
{uiTextToFormattedMessage(node.meta.label, intl)}
</span>
<span className="size-5 block"></span>
</>
) : null}
</button>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

import { UiNode, UiNodeInputAttributes } from "@ory/client-fetch"
import { OrySettingsOidcProps } from "@ory/elements-react"
import { useEffect, useState } from "react"
import { useEffect } from "react"
import { useFormContext } from "react-hook-form"
import { useDebounceValue } from "usehooks-ts"
import Trash from "../../assets/icons/trash.svg"
import logos from "../../provider-logos"
import { DefaultHorizontalDivider } from "../form/horizontal-divider"
Expand Down Expand Up @@ -53,33 +54,31 @@
}

function UnlinkRow({ button }: UnlinkRowProps) {
const [clicked, setClicked] = useState(false)
// Safari cancels form submission events, if we do a state update in the same tick
// so we delay the state update by 100ms
const [clicked, setClicked] = useDebounceValue(false, 100)

Check warning on line 59 in packages/elements-react/src/theme/default/components/settings/settings-oidc.tsx

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/theme/default/components/settings/settings-oidc.tsx#L59

Added line #L59 was not covered by tests
const {
formState: { isSubmitting },
} = useFormContext()
const attrs = button.attributes as UiNodeInputAttributes
const provider = extractProvider(button.meta.label?.context) ?? ""
const Logo = attrs.value in logos ? logos[attrs.value] : logos.generic
const Logo = logos[(attrs.value as string).split("-")[0]]

Check warning on line 65 in packages/elements-react/src/theme/default/components/settings/settings-oidc.tsx

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/theme/default/components/settings/settings-oidc.tsx#L65

Added line #L65 was not covered by tests

const localOnClick = () => {
button.onClick()
// Safari cancels form submission events, if we do a state update in the same tick
// so we delay the state update by 100ms
setTimeout(() => {
setClicked(true)
}, 100)
setClicked(true)

Check warning on line 69 in packages/elements-react/src/theme/default/components/settings/settings-oidc.tsx

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/theme/default/components/settings/settings-oidc.tsx#L69

Added line #L69 was not covered by tests
}

useEffect(() => {
if (!isSubmitting) {
setClicked(false)
}
}, [isSubmitting])
}, [isSubmitting, setClicked])

return (
<div key={attrs.value} className="flex justify-between">
<div className="flex items-center gap-6">
<Logo size={32} />
{Logo ? <Logo size={32} /> : <logos.generic size={32} />}
<p className="text-sm font-medium text-interface-foreground-default-secondary">
{provider}
</p>
Expand Down
Loading