Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@splinetool/react-spline

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@splinetool/react-spline

> A component to easily run Spline scenes in React apps.

  • 0.0.8
  • npm
  • Socket score

Version published
Weekly downloads
20K
increased by3.55%
Maintainers
2
Weekly downloads
 
Created
Source

react-spline

A component to easily run Spline scenes in React apps.

Table of Contents

Install

yarn add @splinetool/react-spline @splinetool/runtime

or

npm install @splinetool/react-spline @splinetool/runtime

Usage

  1. Go to the Export panel and select "React Component", then press "Export".
  2. Spline generates a link for Development (Drafts) and Production.
    1. Drafts are generated each time you press on "Generate Draft". This will create a new link.
    2. All previous drafts are stored under the "Drafts" tab.
    3. You can use the drafts to try ideas, and once you are ready, you can promote your changes to production.

Start using react-spline component in React

import { Spline } from '@splinetool/react-spline';

function App() {
  return (
    <main>
      <Spline scene="[DRAFT OR PROD URL]" />
    </main>
  );
}

Start using react-spline component in Nextjs

  1. Create a wrapped component to be able to use ref.

    import { Spline } from '@splinetool/react-spline';
    import { Ref } from 'react';
    
    export function WrappedSpline({ splineRef, ...props }) {
      return <Spline ref={splineRef} {...props} />;
    }
    
  2. Use next/dynamic to import client-side component.

    import dynamic from 'next/dynamic';
    
    const WrappedSpline = dynamic(
      () => import('./WrappedSpline').then((mod) => mod.WrappedSpline),
      {
        ssr: false,
      }
    );
    
    const Spline = forwardRef((props, ref) => {
      return <WrappedSpline {...props} splineRef={ref} />;
    });
    
    function App() {
      return (
        <main>
          <Spline scene="[DRAFT OR PROD URL]" />
        </main>
      );
    }
    

Listen to react-spline events

Spline is in charged of adding events and user just need to add handler function.

import { Spline } from '@splinetool/react-spline';

function App() {
  function handleMouseDown(e) {
    if (e.target.id === 'my-object-id') {
      doSomething();
    }
  }

  return (
    <main>
      <Spline scene="[DRAFT OR PROD URL]" onMouseDown={handleMouseDown} />
    </main>
  );
}

Trigger scene events from outside

  • Option 1: Using emitEvent with event name and object id.

    import { Spline } from '@splinetool/react-spline';
    
    function App() {
    	const splineRef = useRef();
    
    	function handleTriggerButtonClick() {
    		splineRef.current.emitEvent('mouseHover', 'my-object-id');
    	}
    
    	return (
    		<main>
    			<Spline ref={splineRef} scene="[DRAFT OR PROD URL]"/>
    			<button type="button" onClick={handleTriggerButtonClick}/>
    				Trigger Button
    			</button>
    		</main>
    	)
    }
    
    
  • Option 2: Querying object and trigger event using emitEvent object method.

    import { Spline } from '@splinetool/react-spline';
    
    function App() {
    	const splineRef = useRef();
    	const [myObj, setMyObj] = useState(null);
    
    	useEffect(() => {
    		const obj = splineRef.current.findObjectById('my-object-id')
    		setMyObj(obj)
    	}, [splineRef])
    
    	function handleTriggerButtonClick() {
    		myObj.emitEvent('mouseHover');
    	}
    
    	return (
    		<main>
    			<Spline ref={splineRef} scene="[DRAFT OR PROD URL]"/>
    			<button type="button" onClick={handleTriggerButtonClick}/>
    				Trigger Button
    			</button>
    		</main>
    	)
    }
    

Read and modify spline objects

import { Spline } from '@splinetool/react-spline';

function App() {
	const splineRef = useRef();
	const [myObj, setMyObj] = useState(null);

	useEffect(() => {
		const obj = splineRef.current.findObjectById('my-object-id')
		setMyObj(obj)
	}, [splineRef])

	function handleClick() {
		console.log(myObj) // SPE Proxy Object => { name: 'my object', id: '8E8C2DDD-18B6-4C54-861D-7ED2519DE20E', ... }
	}

	return (
		<main>
			<Spline ref={splineRef} scene="[DRAFT OR PROD URL]"/>
			<button type="button" onClick={handleClick}/>
				Log {myObj.name} info
			</button>
		</main>
	)
}

API

Spline Component Props

NameTypeDescription
scenestringScene file
id?stringCanvas id
style?stringCSS style
className?stringCSS classes
onMouseDown?(e: SplineEvent) => voidFunction handler for Mouse Down events
onMouseHover?(e: SplineEvent) => voidFunction handler for Mouse Hover events
onMouseUp?(e: SplineEvent) => voidFunction handler for Mouse Up events
onKeyDown?(e: SplineEvent) => voidFunction handler for Key Down events
onKeyUp?(e: SplineEvent) => voidFunction handler for Key Up events
onStart?(e: SplineEvent) => voidFunction handler for Start events
onLookAt?(e: SplineEvent) => voidFunction handler for Look At events
onFollow?(e: SplineEvent) => voidFunction handler for Mouse Up events

Spline Container Ref Methods

NameTypeDescription
emitEvent(eventName: string, uuid:string) => voidTriggers a Spline event associated to an object with provided uuid in reverse order. Starts from first state to last state.
emitEventReverse(eventName: string, uuid:string) => voidTriggers a Spline event associated to an object with provided uuid in reverse order. Starts from last state to first state.
findObjectById(uuid: string) => SPEObjectSearches through scene's children and returns the object with that uuid.
findObjectByName(name: string) => SPEObjectSearches through scene's children and returns the first object with that name

Contributing

We use yarn, install the dependencies like this:

yarn

For now, you need to use also:

  • yarn add link:path/to/runtime in order to use the updated version that is merge in dev.
  • yarn link and then yarn link @splinetool/react-spline.

Development

Serve the example folder at localhost:3000

yarn dev

Build Library

yarn build

Publish on npm

yarn deploy

FAQs

Package last updated on 07 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc