Giter VIP home page Giter VIP logo

react-social-icons's Introduction

react-social-icons

build status package version package size weekly downloads type definitions

A set of beautiful svg social icons. Easily used in React. No images or external css dependencies. Example

icons for all social networks configured in this library

Install

npm install react-social-icons
yarn add react-social-icons
pnpm add react-social-icons

Usage

Pass in the url prop of your social network, and the icon will be rendered.

import React from 'react'
import ReactDOM from 'react-dom'
import { SocialIcon } from 'react-social-icons'

const Component = <SocialIcon url="https://twitter.com" />
// React v16
ReactDOM.render(Component, document.body)
// React v17+
ReactDOM.createRoot(document.body).render(Component)

See more usage options on the example site.

This library supports TypeScript since v5.2.0. (type declarations)

Code Splitting and Tree Shaking

Reduce the size of bundled code from this library by importing the SocialIcon component directly and only importing the icons you need. Bundled code using only one icon will be many times smaller. Most icons are only a few hundred bytes in size without compression, which shrinks them another ~30%. The size of the bundled library will scale linearly with each icon you import. Many bundlers will tree shake the unused icons from the final code-split bundle.

import { SocialIcon } from 'react-social-icons/component'
import 'react-social-icons/vimeo'
import 'react-social-icons/meetup'
// renders: vimeo icon
<SocialIcon url="www.vimeo.com" />
// renders: meetup icon
<SocialIcon url="www.meetup.com" />
// renders: default icon
<SocialIcon url="www.pinterest.com" />

Props

Property Type Required Description
url String No The rendered component will link to this url and show the social network's icon.
network String No Override which network icon to render
bgColor String No Override the background fill color (defaults to social network's color)
fgColor String No Override the icon's fill color (defaults to transparent)
label String No Set the aria-label attribute on the rendered anchor tag (defaults to the social network's name)
className String No Specify a class to attach to the rendered anchor tag
style Object No Override style properties passed to the rendered anchor tag
href String No Override the link while keeping the icon matching prop url
as String No Override the root element of the component (defaults to 'a')
fallback String No Specify the icon shown when no network matches the url prop

url

Sets the link the anchor element points to and renders the icon associated with the network matching the url.

// renders: vimeo.com
<SocialIcon url="www.vimeo.com" />

network

Overrides the icon rendered by the component.

// renders: github icon
<SocialIcon network="github" />

// renders: github icon
// on click: navigate to vimeo.com
<SocialIcon network="github" url="www.vimeo.com" />

bgColor and fgColor

Overrides the background or foreground fill colors. Defaults to the network's brand color (bg) and transparent (fg).

// renders: default icon
<SocialIcon bgColor="green" fgColor="blue" />

label

Overrides the ARIA attribute on the anchor element. Defaults to network name.

// renders: vimeo icon
<SocialIcon label="my video channel" url="www.vimeo.com" />
// or
<SocialIcon aria-label="my video channel" url="www.vimeo.com" />

className and style

Specify a CSS class and styles for the anchor element. Read more about these special React props.

<SocialIcon className="colorscheme" style={{ color: 'green' }} />

href

Overrides the anchor link. Ignored when the component decides what icon to render.

// renders: default icon
// on click: navigate to github.com
<SocialIcon href="www.github.com" />

href specifies the anchor link while url specifies the rendered icon

// renders: vimeo icon
// on click: navigate to github.com
<SocialIcon href="www.github.com" url="www.vimeo.com" />

as

Set <SocialIcon> to be any html element you want. Defaults to 'a'.

<SocialIcon as="div" />

fallback

Overrides the default icon shown when a network does not match the given URL.

Accepts a network:

<SocialIcon fallback="pinterest" /> // renders pinterest icon

Or an icon definition:

<SocialIcon fallback={{ color, path }} /> // renders custom icon

The other exports

There are other useful functions and objects exported from the SocialIcon library.

networkFor

A function that accepts a url string and returns the matching social network domain name.

import { networkFor } from 'react-social-icons';
import { assert } from 'assert'
assert.equal(networkFor('https://www.pinterest.com'), 'pinterest')

register

A function that accepts the domain name of a social network with an object definition of the icon's paths and color. It will register the social network icon with the <SocialIcon> component, which will have gained the ability to render the icon for your social network, and update uri_regex to match the domain name.

import { register } from 'react-social-icons';
register('mynetwork', {
  color: 'red',
  path: 'path commands' // see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#path_commands
})

social_icons

A map that associates social network names to the icon objects with the network's color and icon paths.

import { social_icons } from 'react-social-icons'
import assert from 'assert'
assert.ok(social_icons instanceof Map)

network_names and getKeys

network_names is a set that stores all the registered social network domain names. getKeys returns an array of the same information.

import { network_names, getKeys } from 'react-social-icons'
import assert from 'assert'
assert.deepEqual(getKeys(), [...network_names])
assert.ok(network_names instanceof Set)

uri_regex

A regex for urls that will match any social network domain names that are registered. (this will not match mailto: links or return the default network, use networkFor instead)

import { uri_regex } from 'react-social-icons'
import assert from 'assert'
assert.equal(uri_regex.exec('https://www.pinterest.com')?.[1], 'pinterest')

Contributing

Contributors are welcome. See CONTRIBUTING.md.

FAQ

How do I open the link in a new tab when the icon is clicked?

Pass the prop target like so: <SocialIcon target="_blank" url="www.vimeo.com" />. All props are forwarded to the underlying element, an anchor.

How do I use code-splitting?

This package packages exposes the component code and icon definitions in separate files with a simple import interface. There are several useful tools that implement features like tree-shaking to reduce the size of bundled code. Certain browsers contain features that let you important un-bundled code directly. An effort has been made to keep distribution code files simple, separate, and small.

with ES6 browser imports

Refer to a list of compatible browsers and import files directly from your own servers or a CDN.

with a bundler

Webpack and Rollup will tree shake any unused code from this package when you are bundling your code.

How do I add a new icon?

Follow the instructions in CONTRIBUTING.md.

How do I change the color on hover?

There are a couple approaches to changing the color of the icon on hover. These can be modified to fit your particular use case by examining what attributes are on the underlying HTML element.

currentColor and className

In a stylesheet, apply two fills to the social icon. One by default, and one on hover.

/* file: app.css */
.custom-class .social-svg-icon {
    fill: green;
}
.custom-class:hover .social-svg-icon {
    fill: red;
}

In your component, set the fgColor prop to currentColor to inherit colors from the stylesheet rather than the inline style rule from the component.

// file: app.js
<SocialIcon className="custom-class" fgColor="currentColor" />

!important override

You can override the fill color by using the !important CSS declaration

/* file: app.css */
.social-svg-icon {
    fill: green !important;
}

And simply use the icon like normal.

// file: app.js
<SocialIcon />

How do I render icons for federated or decentralized social networks?

Specify the network prop of the social network icon you want to render. For example:

<SocialIcon network="mastodon" url="https://techhub.social/" />
// or
<SocialIcon network="misskey" url="https://misskey-hub.net" />

Federated/decentralized social networks can have instances or user accounts hosted on different domains. This can cause the library to not detect the proper network on a naive inspection of the url prop. Refer to the documentation on props.

Tree-shaking with Typescript causes a build error where the type declarations cannot be found

When importing react-social-icons/component in a Typescript project, if your tsconfig.json is misconfigured you may run into the error message TS2307: Cannot find module 'react-social-icons/component' or its corresponding type declarations.

To fix the issue, set "moduleResolution" in your tsconfig.json to "bundler".

The error occurs when the "moduleResolution" property in your Typescript configuration is set to some variant of classic, or node. Tree-shaking is a strategy of Node.js builds targeting a browser environment. They take advantage of a bundler feature provided by tools like webpack or rollup. If you are using react-social-icons in a project targeting a non-browser environment, you should use the .cjs build of this package, which will be resolved automatically if you import from react-social-icons in your project.

// in server projects ("moduleResolution": "node" or "classic")
import { SocialIcon } from 'react-social-icons'
// in browser projects ("moduleResolution": "bundler")
import { SocialIcon } from 'react-social-icons/component'

react-social-icons's People

Contributors

arnabsen avatar avvs avatar couetilc avatar craftykim avatar dejorrit avatar dependabot[bot] avatar dkku1020 avatar dpsmith3 avatar gonzaloscannone avatar imerica avatar ismaelpr94 avatar jace1084 avatar jackytck avatar jaketrent avatar jayeclark avatar jesse4217 avatar jessupjn avatar jwbrew avatar lucaspeferreira avatar markholland avatar mxmason avatar myin142 avatar nilsbremer avatar oakhtar147 avatar piducancore avatar sasagar avatar satoshigangopadhyay avatar schmalzs avatar skratchdot avatar yaodingyd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-social-icons's Issues

Discord icon is not appearing

Hi - thanks for creating this package. For some reason the Discord icon doesn't seem to appear - all the others I tested are fine.

padding social icons

hi i've been messing around with these icons, awesome work btw, and i can't find how to add padding to them to space them evenly. I can do it in developer tools but i'm not sure how exactly the styling works for these whether an external style sheet or inline styling. any idea?

Error @ Social-Icon (ParseDep)

I'm getting this error; let me know if there's anything weird happening to you as well.

Error: Parsing file /Users/jacob/Work/ITH001 - IthacaRenting.com/website/custom/themes/ithaca/node_modules/react-social-icons/lib/social-icon.js: Unexpected token (32:6)
at Deps.parseDeps (/Users/jacob/Work/ITH001 - IthacaRenting.com/website/custom/themes/ithaca/node_modules/browserify/node_modules/module-deps/index.js:436:28)

p.s. sick component.

Import a single icon?

Is there a way to import only the icon(s) you are using, rather than the whole set, to keep the final bundle size lower?

Thanks!

Default to target="_blank"

Great library & exactly what I was after! (fast and simple)

Just some food for thought, in 99% of cases, I'd want a new tab to open (might just be? am I a tab hoarder?).

Maybe an input parameter that defaults to open new tab might be a good idea?

Happy to write up / add to readme if you think it's a good idea :)

Cheers!

link for someone to email you?

Is there a way to have one of the icons lead to a pop up email window? I'm using network="email" to show the icon, but what to place for the URL?

Hover effect

Hi, what would be the recommended way to add hover effect for these icons (change background for example)? Do you have any guidelines?

Recommend Stackoverflow Icon

Seeing as this package that a lot of developers use, I think I could safely recommend the addition of a Stackoverflow icon?

Rename email

I think it is better to rename email. If you use "mailto" it activates the default mail client on the computer.

Add icon's color

The current version only support changing color of the mask. However, it doesn't support replacing the actual logo with arbitrary color.

How can a user create new icons?

Hi Jake,

I would like to make a new object with the mask, icon, and color properties.
Did you use Sketch? It seems that what I get with Photoshop does not play too nicely.

I would like to create an add a Reddit icon to the package.

Thanks for any tips.

Prop for Opening in a Newtab?

I there a way to open the link (leading to a social page) on a new tab - similar to the target="_blank" attribute on a <a> HTML element.

Scaling classNames broken

Apparently after #4 merge, scaling with 'sm', 'med', 'lrg' and 'xlrg' is broken. The styling got moved to inline styles which are overriding the styles. These inline styles will always override a className that's passed in that tries to adjust the height and width. Since we're now inline on the styles, maybe move the sizing override to be inline as well.

Interest in ownership?

Hey Internetz,

Do any of you want to own this library? I'd love to transfer it to a responsible owner.

Please respond to this thread with any interest.

Thanks,
Jake

Add Reddit Icon

I think it would be super helpful to add reddit as one of the social networks. I found where to add this in the code, but I'm not really comfortable enough yet at coming up with the svg code for this. Would be willing to help out on this if someone could point me towards some helpful documentation on how to get the code for the and tags.

network property is case sensitive

The network property is case sensitive so if I insert YouTube it's not going to display any logo. Please let me know if I can create a PR for this.

npm install not --save-dev

Hey,

working with gatsby. I saved the package like in the readme documented as a dev dependency but got the error that it should be saved as a normal dependency, which makes sense I guess?
Or might that be just an error cause I'm working with Gatsby?
Would be crazy if I would be the only one that ran into that issue. Maybe update the Readme.md? I could send a quick PR :)

Please provide distributed (compiled) file

Hi,

This is a great idea!

The only issue is that you're using experimental javascript. Most webpack configs will exclude node_modules which means this will throw an error.

If you wouldn't mind including a compiled version of this for npm, that would be fantastic!

Thanks

fork with typescript and optional icons

@jaketrent So a while back I forked this project to solve the issue of bundle size.

Today I published this fork with typescript and a context provider you can pass the icons you need only please have a look HERE and if it interests you I can submit a PR with the changes.

Thank you for this awesome project ๐Ÿ™

implementing rel="noreferrer"

Hello, I am currently using the latest version of this package available on npm (2.8.1) and am opening this issue per following through on a Lighthouse report I was running against my website, in particular the recommendation that the usage of an anchor tag with the target="_blank" attribute, e.g.

<a href="www.sometime.com" target="_blank">Click me</a>

should also include noopener noreferrer, e.g.

<a href="www.sometime.com" target="_blank" rel="noopener noreferrer">Click me</a>

I see the latest version in GitHub has support for noopener. I would like to make a PR to add noreferrer as well.

From that point, when do you think a new version could be available in GitHub?

Thanks for the great library! โœŒ๏ธ

[Feature Request] - Discord Button

We are using discord on our channel and currently use the widget to connect our users to our community. I think it would be nice to add a discord button pointing to the discord server.

Not working with React 17

Is this maintained? It seems the React update could have broken the dependency. Would like to know if this will be fixed to work with React 17.

npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR! react@"^17.0.1" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"15.x.x || 16.x.x" from [email protected]
npm ERR! node_modules/react-social-icons
npm ERR! dev react-social-icons@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Allow support for className?

Hi,

To avoid redundancy, it would be better if we could provide a class that could provide;

height, width, backgroundColor, color, margins, padding

Would avoid repeated code too...

Question: setting width and height via CSS

Setting the style's width and height to null allows me to use CSS; however, this functionality seems counter-intuitive.

<SocialIcon url={url} style={{ width: null, height: null }} />

@jaketrent Could you elaborate on the move away from SCSS? I ask because hard-coding the style seems to make the library more difficult to utilize for common scenarios.

Support for dark mode

I really like this project, and even more if it supports dark mode. Do you have any plan for supporting dark mode icons in the future? Thanks.

package.json main

I think your package.json needs updated to

"main": "./lib/react-social-icons.js",

ShareUrl

Hi, How can we pass the url we want shared on the social media site?

Proposal: Fallback Icon

I could create a PR for having a custom fallback icon in case the social icon is not found.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.