• This repository has been archived on 01/Nov/2022
  • Stars
    star
    827
  • Rank 55,139 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ”ฎ morph your svg component one into another other

React svg morph

morph your svg components one into another

React Icons

Instalation

npm install react-svg-morph --save

Usage example

import React from 'react';
import ReactDOM from 'react-dom';
import { MorphReplace } from 'react-svg-morph';

class Checked extends React.Component {
    render() {
        return (
            <svg width="24" fill="#00ea00" height="24" viewBox="0 0 24 24">
                <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
            </svg>
        );
    }
}

class CheckBox extends React.Component {
    render() {
        return (
            <svg width="24" height="24" fill="#666666" viewBox="0 0 24 24">
                <path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"/>
            </svg>
        );
    }
}

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            checked: false
        };
    }
    toggleChecked() {
        this.setState({checked: !this.state.checked});
    }
    render() {
        return (
            <div onClick={this.toggleChecked.bind(this)}>
                <MorphReplace width={100} height={100}>
                    {this.state.checked ? <Checked key="checked" /> : <CheckBox key="checkbox" />}
                </MorphReplace>
            </div>
        );
    }
}

ReactDOM.render(<App />, document.getElementById('app'));

Usage with react-native

react-svg-morph only works on ios because react native art package is only available there. Before getting started you will need to include ReactNativeArt into your project, there is good example how to do that on this link http://browniefed.com/blog/2015/05/03/getting-react-art-running-on-react-native/

Everything else is the same only you need to include MorphReplaceNative and MorphTransitionNative from react-svg-morph/native.

For now it works with normal svg tags/components like in example top.

API

<MorphReplace /> or <MorphReplaceNative />

when children element change it will morph from one svg element into another svg element Props:

width: Number

width of the svg element defaults to 40

height: Number

height of the svg element defaults to 40

viewBox: String

viewBox of the svg element default to 0 0 ${width} ${height}

duration: Number

swap animation duration in ms defaults to 350 ms

rotation: String

rotation of the animation available options are clockwise, counterclock, none defaults to clockwise

preserveAspectRatio: String

Attribute preserveAspectRatio indicates whether or not to force uniform scaling. For available options look here. Default value is xMidYMid meet.

easing: function

easing function, default easing is linear

/*
 * Easing Functions - inspired from http://gizma.com/easing/
 * only considering the t value for the range [0, 1] => [0, 1]
 */
var easeInCubic = function(t) {
    return t*t*t;
}

there is default easing functions already provided in src/utils/easing or lib/utils/easing so you can reuse them

import {easeInQuint} from `lib/utils/easing`
<MorphReplace easing={easeInQuint}>
    {this.state.checked ? <Checked /> : <CheckBox />}
</MorphReplace>

children: React.Element

only element you want to display need to be passed as children, when you replace that element with new one animation will be triggered. It's important to provide key prop to child element so MorphReplace know when child is changed.

every other props passed to the element will be passed to svg, so you can also pass normal svg attributes like fill,opaticy,styles...

usage example
import {MorphReplace} from 'react-svg-morph';

render() {
    return (
        <MorphReplace width={100} height={100}>
            {this.state.checked ? <Checked key={'checked'} /> : <CheckBox key={'checkbox'}/>}
        </MorphReplace>
    )
}

<MorphReplaceResize />

same as MorphReplace only you should use this when you have two svg elements that have different viewBox attributes so MorphReplaceResize will normalize their paths before passing it to MorphReplace

<MorphTransition /> or <MorphTransitionNative />

width: Number

width of the svg element defaults to 40

height: Number

height of the svg element defaults to 40

viewBox: String

viewBox of the svg element default to 0 0 ${width} ${height}

viewBox is ignored in react-native

progress: Number

current progress of the svg animation, default to 0

rotation: String

rotation of the animation available options are clockwise, counterclock, none defaults to clockwise

every other props passed to the element will be passed to svg, so you can passs normal svg attributes like fill,opaticy,styles...

children: Object{from: React.Element, to: React.Element}

accept two React elements that need to have svg element inside, it will morph one into another based on progress passed

other props

All other props will be passed to the svg element

usage example
import {MorphTransition} from 'react-svg-morph';

render() {
    return (
        <MorphTransition progres={50} width={100} height={100}>
            {from: <LoveSvg />, to: <LogoSvg />}
        </MorphReplace>
    )
}

it can be also used with react-motion

Related

  • it works with react-icons out of the box because they are all normalized to the same size

License

MIT Copyright (c) Goran Gajic 2015

Credits

svg convert algorithm is adopted from https://github.com/alexk111/SVG-Morpheus.

More Repositories

1

workmode

๐Ÿข command line tool for blocking sites to be more productive
JavaScript
117
star
2

react-icon-base

base icon for https://github.com/gorangajic/react-icons
JavaScript
51
star
3

react-render-to-json

module to figure out what is passed to this.props.children in react
JavaScript
27
star
4

node-toggl

โฐ Command line utility for
JavaScript
10
star
5

pudge

๐Ÿ”ƒ simple node.js hook utility
JavaScript
6
star
6

devurl

๐Ÿ”— convert your ugly urls dev urls from http://localhost:3000 to http://dev.myapp.com
JavaScript
5
star
7

react-introduction-demo

Code from my talk about react at Startit Centar Indjija
JavaScript
5
star
8

steam-id-convertor

Convert SteamIds from 32-bit to 64-bit numbers and vice versa
JavaScript
4
star
9

WeatherApp

React Native aplikacija pravljena na kursu u Startit Centru Indjija
JavaScript
3
star
10

select-text

Simple script to select text in document, just pass it dom element
JavaScript
3
star
11

smart-ellipsis

cut text and add ellipsis/tail without quirks
JavaScript
2
star
12

is-subdomain-valid

simple module to check if subdomain is valid
JavaScript
2
star
13

react-hit-miss

๐ŸŽฏ jsx conditional rendering inspired by react-router v4
JavaScript
2
star
14

nock-timeout

nock freezes when __recordings__ folder is deleted
JavaScript
1
star
15

trace-viewer

HTML
1
star
16

NewsApp

React Native aplikacija pravljena na kursu u Startit Centru Indjija - 01.06.2019
JavaScript
1
star
17

RNTrackingExpensesIncome

JavaScript
1
star
18

NavigationDemoRNKurs

demonstracija kako radi react-navigation
JavaScript
1
star
19

webpack-symbolic-link-bug

demo of the handlebars-loader and webpack bug
JavaScript
1
star
20

ReactNativeTodo

Todo list react native aplikacija pravljena na kursu u Startit Centru Indjija - 08.06.2019
JavaScript
1
star
21

testing-codespaces

JavaScript
1
star
22

promise-creator

small helper function to easly create new promises
JavaScript
1
star
23

facebook-emoticons

facebook script for adding custom emoticons in chat.
JavaScript
1
star
24

retinable

๐Ÿ’ป checks if device have retina display
JavaScript
1
star