2 * Copyright 2003-present Greg Hurrell. All rights reserved.
3 * Licensed under the terms of the MIT license.
10 import DIGITS from './DIGITS';
11 import DynamicField from './DynamicField.react';
12 import Field from './Field.react';
13 import Label from './Label.react';
14 import React from 'react';
15 import Size from './Size.react';
16 import type Value from './Field.react';
17 import convert from './convert';
18 import debounce from 'simple-debounce';
22 const SERIALIZATION_BASE = DIGITS.length;
24 const replaceHistoryState = debounce(
25 value => window.history.replaceState(
28 '#' + convert(value.value, value.base, SERIALIZATION_BASE)
33 function getInitialValue() {
34 // Extract value from URL fragment, if present.
35 const value = window.location.hash.replace(/^#/, '');
36 const validator = new RegExp(`^[${DIGITS}]+$`);
37 if (!validator.test(value)) {
41 base: SERIALIZATION_BASE,
47 export default class App extends React.Component {
51 value: getInitialValue(),
55 _onValueChange = (value: Value) => {
56 this.setState({value});
57 replaceHistoryState(value);
61 this._firstInput.focus();
65 const {value} = this.state;
68 <div className="hextrapolate">
70 Hextrapolate — <Size value={value} />
72 <Label text="Hexadecimal">
75 onValueChange={this._onValueChange}
76 ref={ref => this._firstInput = ref}
80 <Label text="Decimal">
82 onValueChange={this._onValueChange}
89 onValueChange={this._onValueChange}
96 onValueChange={this._onValueChange}
103 onValueChange={this._onValueChange}
108 onValueChange={this._onValueChange}
110 <footer className="hextrapolate-footer">
111 <a href="mailto:greg@hurrell.net">Contact</a>
113 <a href="https://github.com/wincent/hextrapolate">Source</a>