Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add patchMotionTags for legacy browsers which don't support Proxy #1194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/index.ts
@@ -1,3 +1,8 @@
/**
* Proxy patch for `motion` and `m`
*/
export { patchMotionTags } from "./render/dom/patch-motion-tags"

/**
* Components
*/
Expand Down
138 changes: 138 additions & 0 deletions src/render/dom/dom-tags.ts
@@ -0,0 +1,138 @@
// borrowed from react-dom-factories
export const domTags = [
"a",
"abbr",
"address",
"area",
"article",
"aside",
"audio",
"b",
"base",
"bdi",
"bdo",
"big",
"blockquote",
"body",
"br",
"button",
"canvas",
"caption",
"cite",
"code",
"col",
"colgroup",
"data",
"datalist",
"dd",
"del",
"details",
"dfn",
"dialog",
"div",
"dl",
"dt",
"em",
"embed",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hgroup",
"hr",
"html",
"i",
"iframe",
"img",
"input",
"ins",
"kbd",
"keygen",
"label",
"legend",
"li",
"link",
"main",
"map",
"mark",
"menu",
"menuitem",
"meta",
"meter",
"nav",
"noscript",
"object",
"ol",
"optgroup",
"option",
"output",
"p",
"param",
"picture",
"pre",
"progress",
"q",
"rp",
"rt",
"ruby",
"s",
"samp",
"script",
"section",
"select",
"small",
"source",
"span",
"strong",
"style",
"sub",
"summary",
"sup",
"table",
"tbody",
"td",
"textarea",
"tfoot",
"th",
"thead",
"time",
"title",
"tr",
"track",
"u",
"ul",
"var",
"video",
"wbr",
// SVG
"circle",
"clipPath",
"defs",
"ellipse",
"foreignObject",
"g",
"image",
"line",
"linearGradient",
"marker",
"mask",
"path",
"pattern",
"polygon",
"polyline",
"radialGradient",
"rect",
"stop",
"svg",
"text",
"tspan",
]
8 changes: 8 additions & 0 deletions src/render/dom/motion-proxy.ts
Expand Up @@ -4,6 +4,7 @@ import { HTMLRenderState } from "../html/types"
import { SVGRenderState } from "../svg/types"
import { MotionProps } from "../../motion/types"
import { createMotionComponent, MotionComponentConfig } from "../../motion"
import { motionTags } from "./motion-tags"

/**
* I'd rather the return type of `custom` to be implicit but this throws
Expand Down Expand Up @@ -52,6 +53,13 @@ export function createMotionProxy(createConfig: CreateConfig) {
>(createConfig(Component, customMotionComponentConfig))
}

if (typeof Proxy === "undefined") {
return motionTags.reduce(
(acc, tag) => Object.assign(acc, { [tag]: custom(tag) }),
custom
) as typeof custom & DOMMotionComponents
}

/**
* A cache of generated `motion` components, e.g `motion.div`, `motion.input` etc.
* Rather than generating them anew every render.
Expand Down
2 changes: 2 additions & 0 deletions src/render/dom/motion-tags.ts
@@ -0,0 +1,2 @@
// loaded from ./dom-tags
export const motionTags: string[] = []
8 changes: 8 additions & 0 deletions src/render/dom/patch-motion-tags.ts
@@ -0,0 +1,8 @@
import { motionTags } from "./motion-tags"
import { domTags } from "./dom-tags"

// tree-shakable patch for legacy browsers which don't support Proxy
export const patchMotionTags = (() => {
motionTags.push(...domTags)
return () => {}
})()