-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(astro): Add support for custom pages and links (#3987)
- Loading branch information
1 parent
540b720
commit d426de6
Showing
11 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"@clerk/astro": minor | ||
--- | ||
|
||
Add support for custom pages and links in the `<UserProfile />` Astro component. |
22 changes: 22 additions & 0 deletions
22
integration/templates/astro-node/src/pages/custom-pages.astro
This file contains 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,22 @@ | ||
--- | ||
import { UserProfile } from "@clerk/astro/components"; | ||
import Layout from "../layouts/Layout.astro"; | ||
--- | ||
|
||
<Layout title="Custom Pages"> | ||
<div class="w-full flex justify-center"> | ||
<UserProfile path="/custom-pages"> | ||
<UserProfile.Page label="Terms" url="terms"> | ||
<div slot="label-icon">Icon</div> | ||
<div> | ||
<h1>Custom Terms Page</h1> | ||
<p>This is the custom terms page</p> | ||
</div> | ||
</UserProfile.Page> | ||
<UserProfile.Link label="Homepage" url="/"> | ||
<div slot="label-icon">Icon</div> | ||
</UserProfile.Link> | ||
<UserProfile.Page label="security" /> | ||
</UserProfile> | ||
</div> | ||
</Layout> |
This file contains 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 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 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
8 changes: 0 additions & 8 deletions
8
packages/astro/src/astro-components/interactive/UserProfile.astro
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
packages/astro/src/astro-components/interactive/UserProfile/ProfileRenderer.astro
This file contains 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,60 @@ | ||
--- | ||
interface Props { | ||
url: string | ||
label: string | ||
type: 'page' | 'link' | ||
} | ||
const { url, label, type } = Astro.props | ||
let labelIcon = ''; | ||
let content = '' | ||
if (Astro.slots.has('label-icon')) { | ||
labelIcon = await Astro.slots.render('label-icon'); | ||
} | ||
if (Astro.slots.has('default') && type === 'page') { | ||
content = await Astro.slots.render('default'); | ||
} | ||
--- | ||
|
||
<script is:inline define:vars={{ url, label, content, labelIcon, type }}> | ||
// Get the user profile map from window that we set in the `<InternalUIComponentRenderer />`. | ||
const userProfileComponentMap = window.__astro_clerk_component_props.get('user-profile'); | ||
|
||
const userProfile = document.querySelector('[data-clerk-id^="clerk-user-profile"]'); | ||
|
||
const safeId = userProfile.getAttribute('data-clerk-id'); | ||
const currentOptions = userProfileComponentMap.get(safeId); | ||
|
||
const reorderItemsLabels = ['account', 'security']; | ||
const isReorderItem = reorderItemsLabels.includes(label); | ||
|
||
let newCustomPage = { label } | ||
|
||
if (!isReorderItem) { | ||
newCustomPage = { | ||
...newCustomPage, | ||
url, | ||
mountIcon: (el) => { el.innerHTML = labelIcon }, | ||
unmountIcon: () => { /* Implement cleanup if needed */ } | ||
} | ||
|
||
if (type === 'page') { | ||
newCustomPage = { | ||
...newCustomPage, | ||
mount: (el) => { el.innerHTML = content }, | ||
unmount: () => { /* Implement cleanup if needed */ } | ||
} | ||
} | ||
} | ||
|
||
userProfileComponentMap.set(safeId, { | ||
...currentOptions, | ||
customPages: [ | ||
...(currentOptions?.customPages ?? []), | ||
newCustomPage, | ||
] | ||
}) | ||
</script> |
10 changes: 10 additions & 0 deletions
10
packages/astro/src/astro-components/interactive/UserProfile/UserProfile.astro
This file contains 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,10 @@ | ||
--- | ||
import type { UserProfileProps, Without } from '@clerk/types' | ||
type Props = Without<UserProfileProps, 'customPages'> | ||
import InternalUIComponentRenderer from '../InternalUIComponentRenderer.astro' | ||
--- | ||
|
||
<InternalUIComponentRenderer {...Astro.props} component="user-profile" /> | ||
<slot /> |
14 changes: 14 additions & 0 deletions
14
packages/astro/src/astro-components/interactive/UserProfile/UserProfileLink.astro
This file contains 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,14 @@ | ||
--- | ||
import ProfileRenderer from './ProfileRenderer.astro' | ||
interface Props { | ||
url: string | ||
label: string | ||
} | ||
const { url, label } = Astro.props | ||
--- | ||
|
||
<ProfileRenderer label={label} url={url} type="link"> | ||
<slot name="label-icon" slot="label-icon" /> | ||
</ProfileRenderer> |
23 changes: 23 additions & 0 deletions
23
packages/astro/src/astro-components/interactive/UserProfile/UserProfilePage.astro
This file contains 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,23 @@ | ||
--- | ||
import ProfileRenderer from './ProfileRenderer.astro' | ||
type ReorderItemsLabels = 'account' | 'security' | ||
type Props<Label extends string> = { | ||
label: Label | ||
} & (Label extends ReorderItemsLabels | ||
? { | ||
url?: string | ||
} | ||
: { | ||
url: string | ||
} | ||
) | ||
const { url, label } = Astro.props | ||
--- | ||
|
||
<ProfileRenderer label={label} url={url} type="page"> | ||
<slot name="label-icon" slot="label-icon" /> | ||
<slot /> | ||
</ProfileRenderer> |
8 changes: 8 additions & 0 deletions
8
packages/astro/src/astro-components/interactive/UserProfile/index.ts
This file contains 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,8 @@ | ||
import _UserProfile from './UserProfile.astro'; | ||
import UserProfileLink from './UserProfileLink.astro'; | ||
import UserProfilePage from './UserProfilePage.astro'; | ||
|
||
export const UserProfile = Object.assign(_UserProfile, { | ||
Page: UserProfilePage, | ||
Link: UserProfileLink, | ||
}); |