Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

Commit ac050af

Browse files
committed
Properly handle void elements, like inputs.
1 parent ee151f1 commit ac050af

File tree

9 files changed

+72
-37
lines changed

9 files changed

+72
-37
lines changed

README.md

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ import TypeIt from "typeit-react";
2525
export default () => {
2626
return (
2727
<div className="App">
28-
<TypeIt>
29-
This will be typed in a `span` element!
30-
</TypeIt>
28+
<TypeIt>This will be typed in a `span` element!</TypeIt>
3129
</div>
3230
);
3331
};
@@ -41,11 +39,9 @@ The component will allow its children to fully render, and then type whatever HT
4139
import TypeIt from "typeit-react";
4240

4341
// This could be any component that generates HTML.
44-
const SuperStrong = ({children}) => {
45-
return (
46-
<strong style={{fontSize: "80px"}}>{children}</strong>
47-
)
48-
}
42+
const SuperStrong = ({ children }) => {
43+
return <strong style={{ fontSize: "80px" }}>{children}</strong>;
44+
};
4945

5046
export default () => {
5147
return (
@@ -98,13 +94,13 @@ export default () => {
9894

9995
### Fine-Tuning the Instance w/ Companion Methods
10096

101-
TypeIt comes with a set of [special methods](https://typeitjs.com/docs#instance-methods) that let you fine-tune an animation down to the smallest detail. To leverage them here, pass a function as the `onBeforeInit` prop, which will give you access to the instance you can modify with these methods, and then return back to the component before the animation is initialized.
97+
TypeIt comes with a set of [special methods](https://typeitjs.com/docs#instance-methods) that let you fine-tune an animation down to the smallest detail. To leverage them here, pass a function as the `onBeforeInit` prop, which will give you access to the instance you can modify with these methods, and then return back to the component before the animation is initialized.
10298

10399
```javascript
104100
import TypeIt from "typeit-react";
105101

106-
<TypeIt
107-
getBeforeInit={(instance) => {
102+
<TypeIt
103+
getBeforeInit={instance => {
108104
instance
109105
.type("Hi, I'm Alxe")
110106
.pause(750)
@@ -115,7 +111,7 @@ import TypeIt from "typeit-react";
115111
// Remember to return it!
116112
return instance;
117113
}}
118-
/>
114+
/>;
119115
```
120116

121117
### Accessing the Instance After Initalization
@@ -127,26 +123,24 @@ export default () => {
127123
const [buttonText, setButtonText] = useState("Freeze");
128124
const [instance, setInstance] = useState(null);
129125

130-
const toggleFreeze = () => {
131-
if(instance.is('frozen')) {
126+
const toggleFreeze = () => {
127+
if (instance.is("frozen")) {
132128
instance.unfreeze();
133-
setButtonText('Freeze');
129+
setButtonText("Freeze");
134130
return;
135-
}
131+
}
136132

137133
instance.freeze();
138-
setButtonText('Unfreeze');
139-
}
134+
setButtonText("Unfreeze");
135+
};
140136

141137
return (
142138
<div className="App">
143-
<button onClick={toggleFreeze}>
144-
{buttonText}
145-
</button>
139+
<button onClick={toggleFreeze}>{buttonText}</button>
146140

147-
<TypeIt
141+
<TypeIt
148142
options={{ loop: true }}
149-
getAfterInit={(instance) => {
143+
getAfterInit={instance => {
150144
setInstance(instance);
151145
return instance;
152146
}}
@@ -155,7 +149,7 @@ export default () => {
155149
</TypeIt>
156150
</div>
157151
);
158-
}
152+
};
159153
```
160154

161155
## Need Help?

dist/typeit-react.es.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/typeit-react.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "typeit-react",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "React component for the most versatile JavaScript animated typing utility on the planet.",
5+
"homepage": "https://typeitjs.com",
56
"main": "dist/typeit-react.min.js",
67
"module": "dist/typeit-react.es.min.js",
78
"scripts": {
@@ -11,6 +12,11 @@
1112
"prettier": "prettier --write \"**/*.{md,js}\"",
1213
"test": "echo \"Error: no test specified\" && exit 1"
1314
},
15+
"files": [
16+
"dist/",
17+
"src/",
18+
"package-lock.json"
19+
],
1420
"bugs": {
1521
"url": "https://github.com/alexmacarthur/typeit-react/issues"
1622
},

src/helpers/isVoidElement.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Reference: https://html.spec.whatwg.org/multipage/syntax.html#elements-2
2+
const voidElements: string[] = [
3+
"area",
4+
"base",
5+
"br",
6+
"col",
7+
"embed",
8+
"hr",
9+
"img",
10+
"input",
11+
"link",
12+
"meta",
13+
"param",
14+
"source",
15+
"track",
16+
"wbr"
17+
];
18+
19+
/**
20+
* Given the name of an element, check if it's one of the "void" elements,
21+
* which cannot contain any children.
22+
*/
23+
export default (elementName: string): boolean => {
24+
return voidElements.indexOf(elementName.toLowerCase()) > -1;
25+
}

src/index.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { default as TypeItCore } from 'typeit';
3-
const { useRef, useEffect, useState } = React;
3+
const { useRef, useEffect, useState, useMemo } = React;
4+
import isVoidElement from "./helpers/isVoidElement";
45

56
export interface TypeItOptions {
67
strings?: Array<string> | string
@@ -26,6 +27,9 @@ const TypeIt: React.FunctionComponent = (props: TypeItProps) => {
2627
const ref = useRef<HTMLElement|null>(null);
2728
const {options, element, children, getBeforeInit, getAfterInit, ...remainingProps} = props;
2829
const DynamicElement = element;
30+
const elementIsVoid = useMemo(() => {
31+
return isVoidElement(DynamicElement);
32+
}, [DynamicElement]);
2933

3034
/**
3135
* After the component mounts (and any children are rendered),
@@ -67,9 +71,15 @@ const TypeIt: React.FunctionComponent = (props: TypeItProps) => {
6771

6872
return (
6973
<div style={{ opacity: shouldRenderChildren ? 0 : 1}}>
70-
<DynamicElement ref={ref} {...remainingProps}>
71-
{shouldRenderChildren && children}
72-
</DynamicElement>
74+
{elementIsVoid
75+
? <DynamicElement ref={ref} {...remainingProps} />
76+
:
77+
(
78+
<DynamicElement ref={ref} {...remainingProps}>
79+
{shouldRenderChildren && children}
80+
</DynamicElement>
81+
)
82+
}
7383
</div>
7484
)
7585
}
File renamed without changes.
File renamed without changes.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"target": "es5",
1616
"typeRoots": [
1717
"./node_modules/@types",
18-
"./types"
18+
"src/types"
1919
]
2020
},
2121
"exclude": [

0 commit comments

Comments
 (0)