Skip to content
Open
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
45 changes: 31 additions & 14 deletions src/social-share-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const ANALYTICS_SCHEMA_VERSION = "1.0";
class SocialShareButton {
constructor(options = {}) {
// Resolve container element early to prevent duplicate instances
const containerEl = SocialShareButton._resolveContainer(options.container);
const containerEl = SocialShareButton._resolveContainer(
options.container,
options.debug
);

if (containerEl && containerEl._socialShareButtonInstance) {
return containerEl._socialShareButtonInstance;
Expand Down Expand Up @@ -533,7 +536,8 @@ class SocialShareButton {
copyBtn.classList.remove("copied");
this.feedbackTimeout = null;
}, 2000);
} catch (_err) {
} catch (error) {
this._debugWarn("fallbackCopy failed", error);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
copyBtn.textContent = "Failed";

// Clear any existing feedback timeout
Expand Down Expand Up @@ -732,10 +736,23 @@ class SocialShareButton {
// ---------------------------------------------------------------------------

// Resolves a raw container value (string or Element) to a DOM Element, or null if absent/SSR.
static _resolveContainer(raw) {

static _resolveContainer(raw, debug = false) {
if (!raw) return null;
if (typeof document === "undefined") return null;
return typeof raw === "string" ? document.querySelector(raw) : raw;
if (typeof raw !== "string") return raw;

try {
return document.querySelector(raw);
} catch (error) {
if (debug) {
// Invalid selectors are only logged when debug mode is enabled.
// eslint-disable-next-line no-console
console.warn("[SocialShareButton] Invalid container selector:", raw, error);
}

return null;
}
Comment on lines +739 to +755

Copy link
Copy Markdown
Contributor

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

Route selector diagnostics through the shared warning helper.

Lines 745-746 introduce another inline console.warn and bypass _debugWarn(), causing invalid selectors to log regardless of the library’s debug setting. Make the helper callable from this static, pre-instance path—such as by passing options.debug into the resolver or using a module-level helper—and keep the no-console suppression in one place.

Based on learnings, debugging logs in src/social-share-button.js should be consolidated through a private _debugWarn helper instead of adding per-line inline console.* calls.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/social-share-button.js` around lines 736 - 748, Update static
_resolveContainer to route invalid-selector diagnostics through the shared
_debugWarn helper instead of calling console.warn directly. Make the helper
usable from this pre-instance path, passing the relevant debug setting such as
options.debug or using a module-level helper, while keeping the no-console
suppression centralized in that helper.

Source: Learnings

}

// Returns the cached host container element, or null.
Expand All @@ -744,16 +761,16 @@ class SocialShareButton {
}

/**
* Logs analytics warnings only when debug mode is enabled.
* @param {string} message - Description of the failed analytics path.
* @param {Error} err - The caught error instance.
*/
_debugWarn(message, err) {
// _debugWarn: emit analytics warnings only in debug mode for visibility.
if (!this.options.debug) return;
// eslint-disable-next-line no-console
console.warn("[SocialShareButton Analytics]", message, err);
}
* Logs debug warnings only when debug mode is enabled.
* @param {string} message - Warning message.
* @param {Error} err - The caught error instance.
*/
_debugWarn(message, err) {
if (!this.options.debug) return;

// eslint-disable-next-line no-console
console.warn("[SocialShareButton]", message, err);
}

/**
* Emits an analytics event through all configured delivery paths.
Expand Down
Loading