-
Notifications
You must be signed in to change notification settings - Fork 459
feat(js): add InviteMembersButton and Clerk.openInviteMembers #9124
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
Open
alexcarpenter
wants to merge
5
commits into
main
Choose a base branch
from
invite-members-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
87fe150
feat(js): add InviteMembersButton and Clerk.openInviteMembers
alexcarpenter 11c1341
ci(js): bump ui.shared.browser.js bundlewatch budget to 42KB
alexcarpenter 227f5d7
fix(react): replay preopenInviteMembers when opened without props
alexcarpenter 1ba9158
feat(js): throw dev error when opening invite members without permission
alexcarpenter 79c2172
docs(repo): note dev-error behavior in changeset
alexcarpenter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --- | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/shared': minor | ||
| '@clerk/react': minor | ||
| '@clerk/nextjs': minor | ||
| '@clerk/ui': minor | ||
| --- | ||
|
|
||
| Add `<InviteMembersButton />`, a control component that opens the organization invite-members form in a modal when clicked, working like `<SignInButton mode="modal">`. | ||
|
|
||
| Wrap your own button (or omit children for a default one). The button requires an active organization and should be rendered for members who can manage memberships (`org:sys_memberships:manage`). Opening it without an active organization or that permission is a no-op in production, and throws a descriptive error in development. | ||
|
|
||
| ```tsx | ||
| import { InviteMembersButton } from '@clerk/nextjs'; | ||
|
|
||
| <InviteMembersButton> | ||
| <button>Invite members</button> | ||
| </InviteMembersButton>; | ||
| ``` | ||
|
|
||
| This also adds `Clerk.openInviteMembers()` and `Clerk.closeInviteMembers()` for opening and closing the modal programmatically. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import type { InviteMembersButtonProps } from '@clerk/shared/types'; | ||
| import React from 'react'; | ||
|
|
||
| import type { WithClerkProp } from '../types'; | ||
| import { assertSingleChild, normalizeWithDefaultValue, safeExecute } from '../utils'; | ||
| import { withClerk } from './withClerk'; | ||
|
|
||
| /** | ||
| * A button component that opens a modal containing the organization invite-members form when | ||
| * clicked. Wrap your own button, or omit children to render a default one. | ||
| * | ||
| * Requires an active organization, and should be rendered for members who can manage memberships | ||
| * (the `org:sys_memberships:manage` permission). Guard with `<Show>` or `useAuth()` if unsure. | ||
| * Clicking it when there is no active organization or the current user lacks that permission is a | ||
| * no-op in production, and throws a descriptive error in development. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * import { InviteMembersButton } from '@clerk/react'; | ||
| * | ||
| * function InviteButton() { | ||
| * return ( | ||
| * <InviteMembersButton> | ||
| * <button>Invite members</button> | ||
| * </InviteMembersButton> | ||
| * ); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export const InviteMembersButton = withClerk( | ||
| ({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<InviteMembersButtonProps>>) => { | ||
| const { appearance, getContainer, component, ...rest } = props; | ||
|
|
||
| children = normalizeWithDefaultValue(children, 'Invite members'); | ||
| const child = assertSingleChild(children)('InviteMembersButton'); | ||
|
|
||
| const clickHandler = () => { | ||
| return clerk.openInviteMembers({ appearance, getContainer }); | ||
| }; | ||
|
|
||
| const wrappedChildClickHandler: React.MouseEventHandler = async e => { | ||
| if (child && typeof child === 'object' && 'props' in child) { | ||
| await safeExecute(child.props.onClick)(e); | ||
| } | ||
| return clickHandler(); | ||
| }; | ||
|
|
||
| const childProps = { ...rest, onClick: wrappedChildClickHandler }; | ||
| return React.cloneElement(child as React.ReactElement<unknown>, childProps); | ||
| }, | ||
| { component: 'InviteMembersButton', renderWhileLoading: true }, | ||
| ); |
109 changes: 109 additions & 0 deletions
109
packages/react/src/components/__tests__/InviteMembersButton.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import { userEvent } from '@testing-library/user-event'; | ||
| import React from 'react'; | ||
| import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { InviteMembersButton } from '../InviteMembersButton'; | ||
|
|
||
| const mockOpenInviteMembers = vi.fn(); | ||
| const originalError = console.error; | ||
|
|
||
| const mockClerk = { | ||
| openInviteMembers: mockOpenInviteMembers, | ||
| } as any; | ||
|
|
||
| vi.mock('../withClerk', () => { | ||
| return { | ||
| withClerk: (Component: any) => (props: any) => { | ||
| return ( | ||
| <Component | ||
| {...props} | ||
| clerk={mockClerk} | ||
| /> | ||
| ); | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| describe('<InviteMembersButton/>', () => { | ||
| beforeAll(() => { | ||
| console.error = vi.fn(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| console.error = originalError; | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| mockOpenInviteMembers.mockReset(); | ||
| }); | ||
|
|
||
| it('calls clerk.openInviteMembers when clicked', async () => { | ||
| render(<InviteMembersButton />); | ||
| const btn = screen.getByText('Invite members'); | ||
|
|
||
| await userEvent.click(btn); | ||
| await waitFor(() => { | ||
| expect(mockOpenInviteMembers).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| it('forwards appearance to clerk.openInviteMembers', async () => { | ||
| const appearance = { elements: { rootBox: 'test' } }; | ||
| render(<InviteMembersButton appearance={appearance} />); | ||
| const btn = screen.getByText('Invite members'); | ||
|
|
||
| await userEvent.click(btn); | ||
| await waitFor(() => { | ||
| expect(mockOpenInviteMembers).toHaveBeenCalledWith(expect.objectContaining({ appearance })); | ||
| }); | ||
| }); | ||
|
|
||
| it('renders passed button and calls both click handlers', async () => { | ||
| const handler = vi.fn(); | ||
| render( | ||
| <InviteMembersButton> | ||
| <button | ||
| onClick={handler} | ||
| type='button' | ||
| > | ||
| custom button | ||
| </button> | ||
| </InviteMembersButton>, | ||
| ); | ||
| const btn = screen.getByText('custom button'); | ||
|
|
||
| await userEvent.click(btn); | ||
| await waitFor(() => { | ||
| expect(handler).toHaveBeenCalled(); | ||
| expect(mockOpenInviteMembers).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| it('uses text passed as children', async () => { | ||
| render(<InviteMembersButton>text</InviteMembersButton>); | ||
| screen.getByText('text'); | ||
| }); | ||
|
|
||
| it('throws if multiple children provided', async () => { | ||
| expect(() => { | ||
| render( | ||
| <InviteMembersButton> | ||
| <button type='button'>1</button> | ||
| <button type='button'>2</button> | ||
| </InviteMembersButton>, | ||
| ); | ||
| }).toThrow(); | ||
| }); | ||
|
|
||
| it('does not pass appearance prop to child element', () => { | ||
| const { container } = render( | ||
| <InviteMembersButton appearance={{ elements: { rootBox: 'test' } }}> | ||
| <button type='button'>Invite</button> | ||
| </InviteMembersButton>, | ||
| ); | ||
|
|
||
| const button = container.querySelector('button'); | ||
| expect(button?.hasAttribute('appearance')).toBe(false); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Remove untyped
anyfrom the test mocks.The mocked Clerk object, component, and props use
any, disabling compile-time validation of the new public component contract. Use typed mocks orunknownwith narrowing.As per coding guidelines,
anyis disallowed without justification.🤖 Prompt for AI Agents
Source: Coding guidelines