View and edit binary data in React.
yarn:
yarn add react-hex-editor
npm:
npm install --save react-hex-editor
This uses styled-components
for ease of theming. It's a peer dependency, so don't forget to make
sure it's installed!
import HexEditor from 'react-hex-editor';
import oneDarkPro from 'react-hex-editor/themes/oneDarkPro';
const YourApp = () => {
// `data` contains the bytes to show. It can also be `Uint8Array`!
const data = React.useMemo(() => new Array(100).fill(0), []);
// If `data` is large, you probably want it to be mutable rather than cloning it over and over.
// `nonce` can be used to update the editor when `data` is reference that does not change.
const [nonce, setNonce] = useState(0);
// The callback facilitates updates to the source data.
const handleSetValue = React.useCallback((offset, value) => {
data[offset] = value;
setNonce(v => (v + 1));
}, [data]);
return (
<HexEditor
columns={0x10}
data={data}
nonce={nonce}
onSetValue={handleSetValue}
theme={{ hexEditor: oneDarkPro }}
/>
)
};
If you'd rather provide your own CSS and skip the styled-components
depenency, that's okay too.
There are a few inline styles baked into the component for the basic functionality, but you're free
to modify the fonts and colors to suit your tastes. If you'd like to disable the inline styles and
use an all-CSS solution, pass an empty object for the inlineStyles
prop.
import { UnstyleHexEditor } from 'react-hex-editor';
const YourApp = () => {
/* ... */
return (
<UnstyleHexEditor
className="your-hex-editor"
columns={0x10}
data={data.current}
inlineStyles={{}} // Optional: override or disable inline styles
nonce={nonce}
onSetValue={handleSetValue}
/>
)
};
Refer to styles.ts for an example of how to style. The permutations of combined classnames for each hex editor component really adds up, so you probably want to use a CSS preprocessor that supports nesting.
The default HexEditor
and UnstyledHexEditor
components infer as much as necessary to calculate
various dimensions based on the provided props. Underlying this is a BaseHexEditor
component that
needs everything spelled out.
import { BaseHexEditor /* ot UnstyledBaseHexEditor */ } from 'react-hex-editor';
const YourApp = () => {
/* ... */
return (
<BaseHexEditor
className="your-hex-editor"
columns={0x10} // REQUIRED
data={data.current}
height={500} // REQUIRED
nonce={nonce}
rowHeight={22} // REQUIRED
rows={0x10} // REQUIRED
onSetValue={handleSetValue}
width={600} // REQUIRED
/>
)
};
You probably don't need to use this.
Classname applied to the Hex Editor's root element.
Classnames applied to various components of the Hex Editor that can be used for styling.
Uint8Array
or array of integerss (0-255) to display in the hex editor.
If data
is mutable, update the nonce
when there is a change so that the editor re-renders
with the latest data.
Callback that is invoked with offset
and value
when attempting to write a value. Typically,
you'd want to set data[offset] = value
here.
Whether to show the ascii representation of the data.
Whether to show a fixed row at the top of the table for column offset labels.
Whether to show row offset labels.
Set to true
to add a classname for the currently active column if you want to style it.
Note: This can lead to degraded performance when there are a lot of rows visible.
Function that takes the byte value and converts it to its ASCII representation. You can override it to change the ASCII output.
Controls the number of rows rendered outside of the viewport. Higher numbers will reduce blank areas when scrolling quickly, but may degrade performance.
Number of columns to display. (Can be inferred by auto-sizing component using width.)
Number of rows to display. (Can be inferred by auto-sizing component using height.)
Pixel width of the overall hex editor.
Pixel height of the overall hex editor.
Pixel height of a single row.
Pixel width of a single byte.
Pixel width of a single ASCII character.
Pixel width of the gutter between sections.
Pixel width of the scrollbar.
Includes various keys that are used for fonts and colors of the Hex Editor.
Inline styles applied to the sample components used to infer size. Used for making them invisible and non-interactive.
Set to null
to disable the default styles.
Did you know? There is an input that handles key events, focus, and copy/paste for the Hex Editor! These are the inline styles applied to that element that make it invisible and cover the editor.
Set to null
to disable the default styles
Inline styles applied to the editor root.
Inline styles for various editor components.
Inline styles applied to a single ASCII character. Used for setting flexbox alignment.
Inline styles applied to the ASCII section of a row. Used for setting flexbox alignment.
Inline styles applied to Hex Editor body area. Used to force the scrollbar.
Inline styles applied to a single byte. Used for setting flexbox alignment.
Inline styles applied to the byte section of a row. Used for setting flexbox alignment.
Inline styles applied to the editor root. Used for setting position
and the font-family
.
Inline styles applied to the gutter. Unstyled by default.
Inline styles applied to Hex Editor header area. Used to force the scrollbar.
Inline styles applied to an offset label. Used for setting flexbox alignment.
Inline styles applied to a single row. Used for setting flexbox alignment.
Set to true
to focus the Hex Editor on mount.
Callback for when focus is lost.
Callback for when focus is gained.
Set to true
if the Hex Editor is more of a Hex Viewer.
Set it, if you want!
The following functions are available is you use a ref
on the Hex Editor.
Drops focus from the Hex Editor.
Takes focus for the Hex Editor.
Scroll to the specified pixel offset.
Scroll to the specified row offset. Align is from react-window and accepts 'auto'
, 'smart'
,
'center'
, 'end'
, or 'start'
.
Selects a range in the Hex Editor.
Sets a value in the Hex Editor.
- Improve documentation
- Improve clipboard support
- Undo/redo history
- Text/byte search
- Demo page
Thank you for your interest! If you find a bug or want to add a new feature, open an issue or create a pull request, and we'll figure it out from there.
ISC © Keith McKnight