Skip to content

docs: update examples to use newer React syntax #119

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

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ npm i -S react-signature-canvas

## Usage

```javascript
```jsx
import React from 'react'
import ReactDOM from 'react-dom'
import { createRoot } from 'react-dom/client'
import SignatureCanvas from 'react-signature-canvas'

ReactDOM.render(
createRoot(
document.getElementById('my-react-container')
).render(
<SignatureCanvas penColor='green'
canvasProps={{width: 500, height: 200, className: 'sigCanvas'}} />,
document.getElementById('react-container')
)
```

Expand Down Expand Up @@ -80,10 +81,17 @@ Of these props, all, except for `canvasProps` and `clearOnResize`, are passed th

### API

All API methods require a ref to the SignatureCanvas in order to use and are instance methods of the ref.
All API methods require [a ref](https://react.dev/learn/manipulating-the-dom-with-refs) to the SignatureCanvas in order to use and are instance methods of the ref.

```javascript
<SignatureCanvas ref={(ref) => { this.sigCanvas = ref }} />
```jsx
import React, { useRef } from 'react'
import SignatureCanvas from 'react-signature-canvas'

function MyApp() {
const sigCanvas = useRef(null);

return <SignatureCanvas ref={sigCanvas} />
}
```

- `isEmpty()` : `boolean`, self-explanatory
Expand Down
42 changes: 16 additions & 26 deletions example/src/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
import React, { Component } from 'react'
import React, { useState, useRef } from 'react'
import { createRoot } from 'react-dom/client'

import SignaturePad from '../../src/index.tsx'
import SignatureCanvas from '../../src/index.tsx'

import * as styles from './styles.module.css'

class App extends Component {
state = { trimmedDataURL: null }
function App () {
const sigCanvas = useRef(null)
const [trimmedDataURL, setTrimmedDataURL] = useState(null)

sigPad = {}

clear = () => {
this.sigPad.clear()
function clear () {
sigCanvas.current.clear()
}

trim = () => {
this.setState({
trimmedDataURL: this.sigPad.getTrimmedCanvas().toDataURL('image/png')
})
function trim () {
setTrimmedDataURL(sigCanvas.current.getTrimmedCanvas().toDataURL('image/png'))
}

render () {
const { trimmedDataURL } = this.state
return <div className={styles.container}>
return (
<div className={styles.container}>
<div className={styles.sigContainer}>
<SignaturePad canvasProps={{ className: styles.sigPad }}
ref={(ref) => { this.sigPad = ref }} />
<SignatureCanvas canvasProps={{ className: styles.sigCanvas }} ref={sigCanvas} />
</div>
<div>
<button className={styles.buttons} onClick={this.clear}>
Clear
</button>
<button className={styles.buttons} onClick={this.trim}>
Trim
</button>
<button className={styles.buttons} onClick={clear}>Clear</button>
<button className={styles.buttons} onClick={trim}>Trim</button>
</div>
{trimmedDataURL
? <img className={styles.sigImage} alt='signature'
src={trimmedDataURL} />
? <img className={styles.sigImage} alt='signature' src={trimmedDataURL} />
: null}
</div>
}
)
}

createRoot(document.getElementById('container')).render(<App />)
2 changes: 1 addition & 1 deletion example/src/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ body {
background-color: #fff;
}

.sigPad {
.sigCanvas {
width: 100%;
height: 100%;
}
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,5 @@
"jest-environment-jsdom": {
"canvas": "$canvas"
}
},
"ts-standard": {
"ignore": [
"example"
]
}
}