Skip to content

Commit

Permalink
refactor: remove jsx-runtime from hook and highlighter. (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
morganney authored Jul 21, 2024
1 parent 3316d00 commit a779a93
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 103 deletions.
72 changes: 11 additions & 61 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -1,85 +1,35 @@
# Examples

### UMD

Using `tts-react` from a CDN:

```html
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>tts-react UMD example</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://unpkg.com/tts-react@3.0.0/dist/umd/tts-react.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const root = ReactDOM.createRoot(document.getElementById('root'))
const { TextToSpeech, useTts } = TTSReact
const CustomTTS = ({ children }) => {
const { play, ttsChildren } = useTts({ children })
return (
<>
<button onClick={() => play()}>Play</button>
<div>{ttsChildren}</div>
</>
)
}
root.render(
<>
<CustomTTS>
<p><code>useTts</code> as a UMD module.</p>
</CustomTTS>
<TextToSpeech markTextAsSpoken>
<p><code>TextToSpeech</code> as a UMD module.</p>
</TextToSpeech>
</>
)
</script>
</body>
</html>
```

### Import Map

Uses [htm](https://github.com/developit/htm) and [import maps](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap):
Using `tts-react` with ESM from a CDN and [import maps](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap):

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script type="importmap">
{
"imports": {
"react": "https://esm.sh/react",
"react-dom/": "https://esm.sh/react-dom/",
"tts-react": "https://esm.sh/tts-react",
"htm/": "https://esm.sh/htm/"
"react": "https://esm.sh/react@rc",
"react-dom/": "https://esm.sh/react-dom@rc/",
"react/jsx-runtime": "https://esm.sh/react@rc/jsx-runtime",
"tts-react": "https://esm.sh/tts-react@next"
}
}
</script>
<title>Import Map (no build): tts-react</title>
<title>ESM / CDN / Import Map</title>
</head>
<body>
<div id="root"></div>
<script type="module">
import { createElement } from 'react'
import { createRoot } from 'react-dom/client'
import { TextToSpeech } from 'tts-react'
import { html } from 'htm/react'
createRoot(document.getElementById('root')).render(
html`
<${TextToSpeech} markTextAsSpoken>
<p>Hello from tts-react.</p>
</${TextToSpeech}>
`
createRoot(document.body).render(
createElement(TextToSpeech, { markTextAsSpoken: true }, 'Hello from tts-react.')
)
</script>
</body>
Expand All @@ -90,7 +40,7 @@ Uses [htm](https://github.com/developit/htm) and [import maps](https://developer

Counting on command:

```tsx
```jsx
import { useState, useCallback, useEffect } from 'react'
import { useTts } from 'tts-react'

Expand Down
27 changes: 23 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
"@testing-library/user-event": "^14.5.2",
"@types/eslint__js": "^8.42.3",
"@types/jest": "^29.5.12",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.2.1",
"babel-jest": "^29.7.0",
"eslint": "^8.57.0",
Expand All @@ -51,6 +49,8 @@
"jest-environment-jsdom": "^29.7.0",
"prettier": "^3.2.5",
"ts-jest-resolver": "^2.0.1",
"types-react": "^19.0.0-rc.1",
"types-react-dom": "^19.0.0-rc.1",
"typescript": "^5.5.2",
"typescript-eslint": "^8.0.0-alpha.39"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/tts-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tts-react",
"version": "4.0.0-rc.0",
"version": "4.0.0-rc.1",
"description": "React hook and component for converting text to speech using the Web Speech API or Amazon Polly.",
"type": "module",
"main": "dist/index.js",
Expand Down
46 changes: 23 additions & 23 deletions packages/tts-react/src/highlighter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react'
import { useMemo, createElement, Fragment } from 'react'
import type { CSSProperties } from 'react'

interface HighliterProps {
Expand Down Expand Up @@ -27,32 +27,32 @@ const Highlighter = ({ text, mark, color, backgroundColor }: HighliterProps) =>
const parts = textStr.split(regex)

if (parts.length > 1) {
return (
<span>
{parts.map((part, idx) => {
const key = `${part}-${idx}`

if (!part) {
// Happens when the entire text matches the mark
return null
}

if (regex.test(part)) {
return (
<mark key={key} style={markStyle} data-testid="tts-react-mark">
{part}
</mark>
)
}

return <span key={key}>{part}</span>
})}
</span>
return createElement(
'span',
null,
...parts.map((part, idx) => {
const key = `${part}-${idx}`

if (!part) {
// Happens when the entire text matches the mark
return null
}

if (regex.test(part)) {
return createElement(
'mark',
{ key, style: markStyle, 'data-testid': 'tts-react-mark' },
part
)
}

return createElement('span', { key }, part)
})
)
}
}

return <>{text}</>
return createElement(Fragment, null, text)
}

export { Highlighter }
29 changes: 17 additions & 12 deletions packages/tts-react/src/hook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
useEffect,
Children,
cloneElement,
isValidElement
createElement,
isValidElement,
Fragment
} from 'react'
import type { ReactNode } from 'react'

Expand Down Expand Up @@ -217,18 +219,21 @@ const parseChildrenRecursively = ({
const after = text.substring(end, text.length)

if (found) {
return (
<>
{prev}
<Highlighter
text={found}
mark={stripPunctuation(found)}
color={markColor}
backgroundColor={markBackgroundColor}
/>
{after}
</>
const Highlight = createElement(Highlighter, {
text: found,
mark: stripPunctuation(found),
color: markColor,
backgroundColor: markBackgroundColor
})
const Highlighted = createElement(
Fragment,
{ key: `tts-${start}-${end}` },
prev,
Highlight,
after
)

return Highlighted
}
}
}
Expand Down

0 comments on commit a779a93

Please sign in to comment.