generated from StabilityNexus/Template-Repo
-
-
Notifications
You must be signed in to change notification settings - Fork 41
feat: add Solid.js wrapper component #80
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
Muneerali199
wants to merge
2
commits into
AOSSIE-Org:main
Choose a base branch
from
Muneerali199:feat/solid-js-wrapper
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,115 @@ | ||
| import { onMount, onCleanup, createEffect } from 'solid-js'; | ||
|
|
||
| /** | ||
| * Solid.js wrapper for SocialShareButton. | ||
| * | ||
| * Mirrors the React wrapper (social-share-button-react.jsx) using Solid.js | ||
| * primitives: onMount / onCleanup for lifecycle and createEffect for reactive | ||
| * prop updates. | ||
| * | ||
| * SSR Safety | ||
| * ---------- | ||
| * SolidStart pre-renders on the server, so all browser-API access is guarded | ||
| * with `typeof window !== 'undefined'` inside onMount (which only runs on the | ||
| * client). | ||
| * | ||
| * Usage | ||
| * ----- | ||
| * import SocialShareButton from './social-share-button-solid'; | ||
| * | ||
| * <SocialShareButton | ||
| * url="https://your-website.com" | ||
| * title="Check this out!" | ||
| * description="An amazing website" | ||
| * theme="dark" | ||
| * buttonText="Share" | ||
| * /> | ||
| */ | ||
| export default function SocialShareButton(props) { | ||
| let container; | ||
| let shareButton; | ||
|
|
||
| // Resolve URL and title once, with SSR-safe fallbacks | ||
| const currentUrl = () => props.url || (typeof window !== 'undefined' ? window.location.href : ''); | ||
| const currentTitle = () => props.title || (typeof document !== 'undefined' ? document.title : ''); | ||
|
|
||
| const defaultPlatforms = ['whatsapp', 'facebook', 'twitter', 'linkedin', 'telegram', 'reddit']; | ||
|
|
||
| const buildOptions = () => ({ | ||
| container, | ||
| url: currentUrl(), | ||
| title: currentTitle(), | ||
| description: props.description ?? '', | ||
| hashtags: props.hashtags ?? [], | ||
| via: props.via ?? '', | ||
| platforms: props.platforms ?? defaultPlatforms, | ||
| theme: props.theme ?? 'dark', | ||
| buttonText: props.buttonText ?? 'Share', | ||
| customClass: props.customClass ?? '', | ||
| buttonColor: props.buttonColor ?? '', | ||
| buttonHoverColor: props.buttonHoverColor ?? '', | ||
| onShare: props.onShare ?? null, | ||
| onCopy: props.onCopy ?? null, | ||
| buttonStyle: props.buttonStyle ?? 'default', | ||
| modalPosition: props.modalPosition ?? 'center', | ||
| }); | ||
|
|
||
| onMount(() => { | ||
| // Guard: only run in the browser (SolidStart SSR safety). | ||
| // If the CDN script has not yet executed, retry on a short interval | ||
| // until window.SocialShareButton becomes available. | ||
| if (typeof window === 'undefined') return; | ||
|
|
||
| if (window.SocialShareButton) { | ||
| shareButton = new window.SocialShareButton(buildOptions()); | ||
| return; | ||
| } | ||
|
|
||
| // CDN not yet loaded — poll until it is, then initialise | ||
| const intervalId = setInterval(() => { | ||
| if (window.SocialShareButton) { | ||
| clearInterval(intervalId); | ||
| shareButton = new window.SocialShareButton(buildOptions()); | ||
| } | ||
| }, 50); | ||
|
|
||
| // Safety: stop polling on cleanup if it never loaded | ||
| onCleanup(() => clearInterval(intervalId)); | ||
| }); | ||
|
|
||
| // Re-apply options whenever any reactive prop changes. | ||
| // Mirrors the same defaults used at mount so a first reactive flush | ||
| // never overwrites initialised values with undefined. | ||
| createEffect(() => { | ||
| if (shareButton) { | ||
| shareButton.updateOptions({ | ||
| url: currentUrl(), | ||
| title: currentTitle(), | ||
| description: props.description ?? '', | ||
| hashtags: props.hashtags ?? [], | ||
| via: props.via ?? '', | ||
| platforms: props.platforms ?? defaultPlatforms, | ||
| theme: props.theme ?? 'dark', | ||
| buttonText: props.buttonText ?? 'Share', | ||
| customClass: props.customClass ?? '', | ||
| buttonColor: props.buttonColor ?? '', | ||
| buttonHoverColor: props.buttonHoverColor ?? '', | ||
| onShare: props.onShare ?? null, | ||
| onCopy: props.onCopy ?? null, | ||
| buttonStyle: props.buttonStyle ?? 'default', | ||
| modalPosition: props.modalPosition ?? 'center', | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // Clean up the instance and its DOM nodes on component removal | ||
| onCleanup(() => { | ||
| if (shareButton) { | ||
| shareButton.destroy(); | ||
| shareButton = null; | ||
| } | ||
| }); | ||
|
|
||
| // The vanilla library mounts inside this <div> | ||
| return <div ref={container}></div>; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.