Switch
is a utility component that lets you switch between different states of an element on the canvas using animated transitions. It comes with an additional SwitchToStateAction
component, which acts as a hotspot that can change the current state of a Switch
without writing any code.
→ View documentation on GitHub
3/26/2021
3/24/2021
→ See examples in Framer Web (requires free account)
→ Download examples (.framerx file)
Interactive Switches can manage their own state. Use them when you need to have a Switch change its own state when it's tapped / hovered over. Here are a few examples of components that are a good fit for an interactive Switch:
Controlled Switches rely on other elements to set their state. Here are a few use cases that are a good fit for a controlled Switch:
SwitchToStateAction
hotspots control the state of your switch.SwitchToStateAction
component onto the canvas. This component will act as a hotspot that changes the state of a Switch component when the user interacts with it.Switch
to the name you used in step 3, then pick a trigger, and a target state.The "Auto-Animate" transition works differently from the other available state transitions. When triggered, elements shared between two states will animate their properties smoothly between each state. If there are elements in the next state that don't exist in the current one, they'll fade in, and elements that should no longer be shown will fade out.
Before an auto-animate transition begins, the Switch component looks for matching elements between the current and the next state. To figure out if an element matches another element, it looks at:
Note: Names of Graphic layers are currently not recognized properly. To transition between Graphic layers, wrap them in a Frame and name the Frame.
If an element is found to be matching another element, we'll create an animated transition for the following properties:
If the two matching elements are of a different type (e.g. a transition between a Frame and a Graphic), or are of a type where a smooth transition isn't possible (e.g. transitions between two Text layers), the two elements will be animated using a cross-dissolve.
Each auto-animate transition groups transitioning elements in 3 categories:
Each group can have different transition timing. Morphing elements always use the timing options defined right below the "Transition" property when it's set to "Auto-Animate". The Enter and Exit transitions will have their own options right below (by default, they're set to a 0.3s dissolve).
Additionally, auto-animate lets you take advantage of Framer Motion's ability to stagger or delay animations of nested elements.
Since code components can render arbitrary HTML, it's hard for Switch to figure out if content rendered by the code component should also be auto-animated. To get around this issue, whenever it encounters a Code Component, the Switch will only animate its position and size. It will then determine if any of the component's props have changed between the different states, and if they have, it will pass the new props to the component.
Matching code components will not get unmounted and remounted between states, just passed new props. This is especially powerful with components that are expensive to fully re-render, like maps and videos.
If this behavior doesn't match what you expect, you can set the morphCodeComponentPropsOnly
prop on a Switch instance to false
through an Override, and we'll fall back to a cross-dissolve between the two states of the code component.
Use these in Overrides, or when you use the Switch
component from code.
Prop | Type | Description |
---|---|---|
autoAssignIdentifier |
boolean | When true , the Switch instance will get its own random identifier automatically. Default: false |
identifier |
string | The name of the Switch instance. Make sure this is unique. |
initialState |
number | The index of the initial state of the Switch. Default: 0 |
overflow |
boolean | Whether content outside the bounds of the container will be shown or not. Default: true |
onSwitch |
function | A callback function that will be called whenever the Switch component switches its state. The function is passed in the current state index, the previous state index, and the identifier of the Switch component in this order. |
shouldTrigger |
function | A callback function that will be called right before a trigger is fired. Returning false from this callback will stop the trigger from firing. All original arguments to the trigger will be passed down to the function (e.g. event ) |
transition |
enum | The transition to use when switching states. Can be one of: instant , autoanimate , dissolve , zoom , zoomout , zoomin , swapup , swapdown , swapleft , swapright , slidehorizontal , slidevertical , slideup , slidedown , slideleft , slideright , pushhorizontal , pushvertical , pushup , pushdown , pushleft , pushright . |
Prop | Type | Description |
---|---|---|
targetType |
enum | Defines whether the SwitchToStateAction will target the closest switch, or target a specifically named Switch. Possible values are: closest , named . Default is named . |
target |
string | The name of the Switch instance to target. Make sure this is unique and matches the name of an existing Switch. Only used when targetType is set to named . |
shouldTrigger |
function | A callback function that will be called right before a trigger is fired. Returning false from this callback will stop the trigger from firing. All original arguments to the trigger will be passed down to the function (e.g. event ) |
useSwitch
HookTo control Switches from code, first import the useSwitch
hook at the top of your file:
import { useSwitch } from "@framer/tishogeorgiev.switch/code"
Note on Framer Web: Sometimes Web will throw an error in the in-app preview saying it can't find useSwitch
. This is because it hasn't had time to reload all of its dependencies. When this happens, refresh your browser and it should be fine.
Note: You will likely get a red underline under @framer/tishogeorgiev.switch/code
, with an error message like "Cannot find module '@framer/tishogeorgiev.switch/code'". You can safely ignore this error, as the code will work anyway. If it bothers you, import useSwitch
like this, instead:
// @ts-ignore
import { useSwitch } from "@framer/tishogeorgiev.switch/code"
Then call the useSwitch()
hook in your code component or override:
export function SwitchButton(): Override {
const controls = useSwitch()
return {
onTap: () => {
controls.setSwitchState("sharedSwitch", 1)
},
}
}
Note: you can only call this hook from inside a React component or override. Calling it from a different place in your code could result in unexpected behavior. Read more about the rules of hook usage.
The useSwitch
hook will return a controls object with the following functions:
controls.getSwitches() => string[]
Returns an array of identifiers for all registered Switches.
controls.getSwitchStateIndex(identifier: string) => number
Returns the index of the current state of a Switch.
controls.setSwitchState(identifier: string, state: string | number)
Sets the current state of a Switch. You can use either the name of the target state (which will be the same as the name of its layer in the Layers panel), or its numerical state index.
controls.setSwitchStateIndex(identifier: string, state: number)
(Deprecated in favor of setSwitchState
) Sets the current state index of a Switch. If the index isn't valid, it will still be accepted, but the targeted Switch will remain set to its last known good state.
controls.setNextSwitchState(identifier: string, options: { wrapAround?: boolean } = {})
Sets the current state of a Switch to the next available one (the order is the same as the order in which you connected the states). The second options
parameter is optional, and allows you to specify whether the state will wrap around if trying to advance past the last one. By default, wrapAround
is true.
Compatibility note: This method used to be called setNextSwitchStateIndex
in versions prior to v1.8.0. The old name is still supported.
controls.setPreviousSwitchState(identifier: string, options: { wrapAround?: boolean } = {})
Sets the current state of a Switch to the previous one (one behind the current one). The second options
parameter is optional, and allows you to specify whether the state will wrap around if trying to go back beyond the first one. By default, wrapAround
is true.
Compatibility note: This method used to be called setPreviousSwitchStateIndex
in versions prior to v1.8.0. The old name is still supported.
controls.getAllSwitchStates(identifier: string) => string[]
Returns an array of all names states for a Switch. Frames that haven't been explicitly named might have undefined
as their name.
controls.registerSwitchStates(identifier: string, states: string[])
Registers a list of named states for a particular Switch identifier. For internal use only.
12/17/2020
10/30/2020
10/07/2020
9/08/2020
I
, then go to the Switch package and wait for the "Update" button to show up). Let me know if you continue to have problems after.9/01/2020
7/17/2020
5/29/2020
5/26/2020
5/14/2020
4/6/2020
3/24/2020
3/23/2020
2/13/2020
2/5/2020
setSwitchState
from switching to an index if the name for the state for that index was undefined.2/4/2020
1/31/2020
1/10/2020
1/8/2020
1/2/2020
setSwitchState
function from code to set a Switch to either a named state, or a numerical index. See more in the Using Switch in Code section.12/15/2019
SwitchToStateAction
) or the Switch component is mounted on the current view.useSwitch
hook: setNextSwitchStateIndex
and setPreviousSwitchStateIndex
, allowing you to quickly set the next/previous state of a Switch.12/8/2019
11/28/2019
11/27/2019
ctrl+o, cmd+shift+t
. Switch uses the popular hotkeys-js library, so take a look at some of the examples on its Github page to get an idea of the syntax.11/26/2019
SwitchToStateAction
now lets you define actions for multiple triggers, similar to a fully interactive Switch. Unfortunately, this change isn't compatible with the previous version of this package, so if you update to the latest version, make sure you rewire all hotspots you're using.useSwitch
hook. More details in the docs below.shouldTrigger
callback to SwitchToStateAction, which will be called whenever a trigger is about to be executed. If shouldTrigger
returns false
, the trigger won't fire. Check out the "Slide to Unlock" example in the updated examples project.11/21/2019
onSwitch
callback to Switch component, which will be called when the current state changes.