(null);
const initRef = useRef(false);
useEffect(() => {
const initButton = () => {
- if (initRef.current || !window.SocialShareButton || !containerRef.current) return;
-
+ if (initRef.current || !window.SocialShareButton || !containerRef.current)
+ return;
+
shareButtonRef.current = new window.SocialShareButton({
- container: '#share-button'
+ container: "#share-button",
});
initRef.current = true;
};
@@ -327,7 +343,10 @@ declare global {
```html
-
+
@@ -339,12 +358,64 @@ declare global {
```javascript
new window.SocialShareButton({
- container: '#share-button'
+ container: "#share-button",
});
```
+
+🚀 Astro
+
+### Step 1: Add CDN to your layout (e.g., `src/layouts/Layout.astro`)
+
+```astro
+---
+// src/layouts/Layout.astro
+---
+
+
+
+
+
+
+
+
+
+
+
+
+```
+
+> **Note:** The `is:inline` directive tells Astro to leave the script tag as-is in the HTML output instead of bundling it, which is required for CDN scripts.
+
+### Step 2: In your **existing** Astro page or component (e.g., `src/pages/index.astro`):
+
+```astro
+---
+import Layout from '../layouts/Layout.astro';
+---
+
+
+
+
+
+```
+
+> **Tip:** Astro pages are server-rendered as static HTML by default, so the CDN-loaded script works seamlessly without any framework adapters.
+
+
+
---
## Configuration
@@ -353,37 +424,37 @@ new window.SocialShareButton({
```jsx
new SocialShareButton({
- container: '#share-button', // Required: CSS selector or DOM element
- url: 'https://example.com', // Optional: defaults to window.location.href
- title: 'Custom Title', // Optional: defaults to document.title
- buttonText: 'Share', // Optional: button label text
- buttonStyle: 'primary', // default | primary | compact | icon-only
- theme: 'dark', // dark | light
- platforms: ['twitter', 'linkedin'] // Optional: defaults to all platforms
+ container: "#share-button", // Required: CSS selector or DOM element
+ url: "https://example.com", // Optional: defaults to window.location.href
+ title: "Custom Title", // Optional: defaults to document.title
+ buttonText: "Share", // Optional: button label text
+ buttonStyle: "primary", // default | primary | compact | icon-only
+ theme: "dark", // dark | light
+ platforms: ["twitter", "linkedin"], // Optional: defaults to all platforms
});
```
### All Available Options
-| Option | Type | Default | Description |
-|--------|------|---------|-------------|
-| `container` | string/Element | - | **Required.** CSS selector or DOM element |
-| `url` | string | `window.location.href` | URL to share |
-| `title` | string | `document.title` | Share title/headline |
-| `description` | string | `''` | Additional description text |
-| `hashtags` | array | `[]` | Hashtags for posts (e.g., `['js', 'webdev']`) |
-| `via` | string | `''` | Twitter handle (without @) |
-| `platforms` | array | All platforms | Platforms to show (see below) |
-| `buttonText` | string | `'Share'` | Button label text |
-| `buttonStyle` | string | `'default'` | `default`, `primary`, `compact`, `icon-only` |
-| `buttonColor` | string | `''` | Custom button background color |
-| `buttonHoverColor` | string | `''` | Custom button hover color |
-| `customClass` | string | `''` | Additional CSS class for button |
-| `theme` | string | `'dark'` | `dark` or `light` |
-| `modalPosition` | string | `'center'` | Modal position on screen |
-| `showButton` | boolean | `true` | Show/hide the share button |
-| `onShare` | function | `null` | Callback when user shares: `(platform, url) => {}` |
-| `onCopy` | function | `null` | Callback when user copies link: `(url) => {}` |
+| Option | Type | Default | Description |
+| ------------------ | -------------- | ---------------------- | -------------------------------------------------- |
+| `container` | string/Element | - | **Required.** CSS selector or DOM element |
+| `url` | string | `window.location.href` | URL to share |
+| `title` | string | `document.title` | Share title/headline |
+| `description` | string | `''` | Additional description text |
+| `hashtags` | array | `[]` | Hashtags for posts (e.g., `['js', 'webdev']`) |
+| `via` | string | `''` | Twitter handle (without @) |
+| `platforms` | array | All platforms | Platforms to show (see below) |
+| `buttonText` | string | `'Share'` | Button label text |
+| `buttonStyle` | string | `'default'` | `default`, `primary`, `compact`, `icon-only` |
+| `buttonColor` | string | `''` | Custom button background color |
+| `buttonHoverColor` | string | `''` | Custom button hover color |
+| `customClass` | string | `''` | Additional CSS class for button |
+| `theme` | string | `'dark'` | `dark` or `light` |
+| `modalPosition` | string | `'center'` | Modal position on screen |
+| `showButton` | boolean | `true` | Show/hide the share button |
+| `onShare` | function | `null` | Callback when user shares: `(platform, url) => {}` |
+| `onCopy` | function | `null` | Callback when user copies link: `(url) => {}` |
**Available Platforms:**
`whatsapp`, `facebook`, `twitter`, `linkedin`, `telegram`, `reddit`, `email`
@@ -394,16 +465,17 @@ Control the text that appears when users share to social platforms:
```jsx
new SocialShareButton({
- container: '#share-button',
- url: 'https://myproject.com',
- title: 'Check out my awesome project!', // Main title/headline
- description: 'An amazing tool for developers', // Additional description
- hashtags: ['javascript', 'webdev', 'opensource'], // Hashtags included in posts
- via: 'MyProjectHandle' // Your Twitter handle
+ container: "#share-button",
+ url: "https://myproject.com",
+ title: "Check out my awesome project!", // Main title/headline
+ description: "An amazing tool for developers", // Additional description
+ hashtags: ["javascript", "webdev", "opensource"], // Hashtags included in posts
+ via: "MyProjectHandle", // Your Twitter handle
});
```
**How messages are customized per platform:**
+
- **WhatsApp:** `title` + `description` + `hashtags` + link
- **Facebook:** `title` + `description` + `hashtags` + link
- **Twitter/X:** `title` + `description` + `hashtags` + `via` handle + link
@@ -418,8 +490,8 @@ new SocialShareButton({
```jsx
new SocialShareButton({
- container: '#share-button',
- buttonStyle: 'primary' // or 'default', 'compact', 'icon-only'
+ container: "#share-button",
+ buttonStyle: "primary", // or 'default', 'compact', 'icon-only'
});
```
@@ -429,9 +501,9 @@ Pass `buttonColor` and `buttonHoverColor` to match your project's color scheme:
```jsx
new SocialShareButton({
- container: '#share-button',
- buttonColor: '#ff6b6b', // Button background color
- buttonHoverColor: '#ff5252' // Hover state color
+ container: "#share-button",
+ buttonColor: "#ff6b6b", // Button background color
+ buttonHoverColor: "#ff5252", // Hover state color
});
```
@@ -441,9 +513,9 @@ For more complex styling, use a custom CSS class:
```jsx
new SocialShareButton({
- container: '#share-button',
- buttonStyle: 'primary',
- customClass: 'my-custom-button'
+ container: "#share-button",
+ buttonStyle: "primary",
+ customClass: "my-custom-button",
});
```
@@ -467,46 +539,46 @@ Then in your CSS file:
```jsx
// Material Design Red
new SocialShareButton({
- container: '#share-button',
- buttonColor: '#f44336',
- buttonHoverColor: '#da190b'
+ container: "#share-button",
+ buttonColor: "#f44336",
+ buttonHoverColor: "#da190b",
});
// Tailwind Blue
new SocialShareButton({
- container: '#share-button',
- buttonColor: '#3b82f6',
- buttonHoverColor: '#2563eb'
+ container: "#share-button",
+ buttonColor: "#3b82f6",
+ buttonHoverColor: "#2563eb",
});
// Custom Brand Color
new SocialShareButton({
- container: '#share-button',
- buttonColor: '#your-brand-color',
- buttonHoverColor: '#your-brand-color-dark'
+ container: "#share-button",
+ buttonColor: "#your-brand-color",
+ buttonHoverColor: "#your-brand-color-dark",
});
```
### Button Styles
-| Style | Description |
-|-------|-------------|
-| `default` | Standard button with icon and text |
-| `primary` | Gradient button (recommended) |
-| `compact` | Smaller size for tight spaces |
-| `icon-only` | Icon without text |
+| Style | Description |
+| ----------- | ---------------------------------- |
+| `default` | Standard button with icon and text |
+| `primary` | Gradient button (recommended) |
+| `compact` | Smaller size for tight spaces |
+| `icon-only` | Icon without text |
### Callbacks
```jsx
new SocialShareButton({
- container: '#share-button',
+ container: "#share-button",
onShare: (platform, url) => {
console.log(`Shared on ${platform}: ${url}`);
},
onCopy: (url) => {
- console.log('Link copied:', url);
- }
+ console.log("Link copied:", url);
+ },
});
```
@@ -517,11 +589,11 @@ new SocialShareButton({
### Using npm Package
```javascript
-import SocialShareButton from 'social-share-button-aossie';
-import 'social-share-button-aossie/src/social-share-button.css';
+import SocialShareButton from "social-share-button-aossie";
+import "social-share-button-aossie/src/social-share-button.css";
new SocialShareButton({
- container: '#share-button'
+ container: "#share-button",
});
```
@@ -530,10 +602,10 @@ new SocialShareButton({
If you want a reusable React component, copy `src/social-share-button-react.jsx` to your project:
```jsx
-import { SocialShareButton } from './components/SocialShareButton';
+import { SocialShareButton } from "./components/SocialShareButton";
function App() {
- return ;
+ return ;
}
```
@@ -543,15 +615,15 @@ function App() {
const shareButton = useRef(null);
useEffect(() => {
- shareButton.current = new window.SocialShareButton({
- container: '#share-button'
+ shareButton.current = new window.SocialShareButton({
+ container: "#share-button",
});
}, []);
useEffect(() => {
if (shareButton.current) {
- shareButton.current.updateOptions({
- url: window.location.href
+ shareButton.current.updateOptions({
+ url: window.location.href,
});
}
}, [pathname]); // Update on route change
@@ -576,9 +648,10 @@ useEffect(() => {
**Cause:** Script loads after component renders
**Solution:** Add null check:
+
```jsx
if (window.SocialShareButton) {
- new window.SocialShareButton({ container: '#share-button' });
+ new window.SocialShareButton({ container: "#share-button" });
}
```
@@ -589,7 +662,8 @@ if (window.SocialShareButton) {
**Cause:** CSS not loaded or ID mismatch
-**Solution:**
+**Solution:**
+
- Verify CSS CDN link in ``
- Match container ID: `container: '#share-button'` = ``
@@ -630,14 +704,14 @@ if (window.SocialShareButton) {
```jsx
// Professional networks only
new SocialShareButton({
- container: '#share-button',
- platforms: ['linkedin', 'twitter', 'email']
+ container: "#share-button",
+ platforms: ["linkedin", "twitter", "email"],
});
// Messaging apps only
new SocialShareButton({
- container: '#share-button',
- platforms: ['whatsapp', 'telegram']
+ container: "#share-button",
+ platforms: ["whatsapp", "telegram"],
});
```
@@ -645,9 +719,9 @@ new SocialShareButton({
```jsx
new SocialShareButton({
- container: '#share-button',
- buttonStyle: 'icon-only',
- theme: 'light'
+ container: "#share-button",
+ buttonStyle: "icon-only",
+ theme: "light",
});
```