Coder Social home page Coder Social logo

joshwcomeau / react-retro-hit-counter Goto Github PK

View Code? Open in Web Editor NEW
161.0 4.0 5.0 6.06 MB

๐Ÿ†• Go back in time with this 90s-style hit counter.

Home Page: https://joshwcomeau.github.io/react-retro-hit-counter/

License: MIT License

JavaScript 100.00%
react reactjs retro counter

react-retro-hit-counter's Introduction

example image

React Retro Hit Counter

npm GitHub code size in bytes npm

Remember when the web was this quirky place full of "Under Construction" clipart, guestbooks, and web rings? The time when animated GIFs were used for everything - background images, cursors, nothing was off-limits. The time before we learned that BonziBuddy was actually malware.

Sometimes I miss the 90s web. This is a small gesture to help share some of that nostalgia.

Relive your youth with this straight-outta-geocities hit counter. If you have a page on the Information SuperHighway, slap this bad boy on it and impress all your friends.

  • ๐Ÿ’พ Retro 90s aesthetic
  • โš™๏ธ Highly customizable
  • ๐Ÿ”ฒ Includes optional skeumorphic border, and soft glow.
  • ๐Ÿ”จ Generated on-the-fly with SVG and Canvas. No images, totally scalable.
  • โšก๏ธ Tiny! <3kb gzip.

Easily the best React counter you'll find anywhere on Altavista.


Is this serious?

Yes! It's a real thing.

ยป Try the demo! ๐ŸŽ‰

Does this actually track hits?

No. This is just a presentational component, you need your own tracking system.

I recommend micro-analytics. Super easy to set up and use.

Compatibility

Compatible with React 15.3 or higher.

Works on all major browsers, including Edge. Haven't tested on IE.

Installation

$ yarn install react-retro-hit-counter
# or
$ npm i -s react-retro-hit-counter

Usage

import RetroHitCounter from 'react-retro-hit-counter';

const YourComponent = () => (
  <RetroHitCounter
    hits={1337}
    /* The following are all default values: */
    withBorder={true}
    withGlow={false}
    minLength={4}
    size={40}
    padding={4}
    digitSpacing={3}
    segmentThickness={4}
    segmentSpacing={0.5}
    segmentActiveColor="#76FF03"
    segmentInactiveColor="#315324"
    backgroundColor="#222222"
    borderThickness={7}
    glowStrength={0.5}
  />
);

API Reference

hits
Type: Default Value
number undefined

The number of hits to display!

Protip: You can cheat, I won't tell! hits={actualNumber * 100}

minLength
Type: Default Value
number 4

Single-digit hit counters are sad. Start-pad the number with 0s.

minLength

size
Type: Default Value
number 40

The height, in pixels, of each digit. Not including padding.

hits

padding
Type: Default Value
number 4

The padding, in pixels, around the hit counter.

padding

digitSpacing
Type: Default Value
number 3

The amount of space, in pixels, between each digit.

digitSpacing

segmentThickness
Type: Default Value
number 4

The width of each segment, in pixels.

segmentThickness

You can make really abstract numbers with this prop! I've decided that this is a feature, not a bug.

segmentSpacing
Type: Default Value
number 0.5

Spacing between the segments, in pixels.

segmentSpacing

segmentActiveColor
Type: Default Value
string #76FF03

Each digit is comprised of 7 segments, and this prop controls the color of the active ones (AKA the number itself).

segmentActiveColor

segmentInactiveColor
Type: Default Value
string #315324

Controls the color of the inactive segments. Pass "transparent" to not show the inactive ones.

segmentInactiveColor

backgroundColor
Type: Default Value
string #222222

Set a custom background color for your hit counter. Accepts any valid CSS value (pass "transparent" for no background).

backgroundColor

withBorder
Type: Default Value
boolean true

No 90s hit counter would be complete without a skeumorphic chrome border!

withBorder

Protip: You can use the border on your own if you like, it's a named export. import {RetroBorder} from 'react-retro-hit-counter'. You'll need to provide an explicit width/height, though (the border is made in Canvas, and I didn't want the cost of reading width/height from the DOM).

You can find its props here

borderThickness
Type: Default Value
number 7

Width, in pixels, of the border. Only used if withBorder is set to true.

borderThickness

withGlow
Type: Default Value
boolean false

If desired, a soft glow can be emitted from the hit counter. This works especially well with the border.

The color of the glow is based on segmentActiveColor.

withGlow

glowSize
Type: Default Value
number 2

The size of the glow. This parameter controls both the negative margin, in pixels, as well as the blur amount (also in pixels). Only used if withGlow is set to true.

glowSize

glowStrength
Type: Default Value
number 0.4

The opacity of the glow. Only used if withGlow is set to true.

glowStrength

Future Work and Contributions

Wanna help bring that 90s aesthetic back to the web?

There are a few things I'd like to do, and could use a hand with:

  • #1 - The segments are all the same shape. This can be harmful to legibility; ideally, you want the outer pieces to be trapezoids. See an example.

  • #2 - a11y: unclear if the best approach is to use aria tags, or just add actual zero-opacity text like "Hit counter. Number: n" that screen-readers can recite.

  • #3 - Tests! I have very few tests, none on the component itself. This should change.

  • #4 - Build process could use some work, specifically making it easier to work on the demo.

  • Additional flourishes. Transitions right now are just a simple CSS transition, and they're the only animation. Possibly this is good enough and anything else would just be fluff... but I'm curious if we can do anything else to make it more interesting?

To set up locally, simply clone and run yarn or npm i. Then, run yarn storybook to get an interactive sandbox. Add stories as needed to test the contribution.

There's also a demo app, which offers far greater parameter control. You can cd into /demo, run yarn to install the demo dependencies, and then yarn start to run the demo. From the parent directory, whenever you yarn build, it updates the demo dependency.

react-retro-hit-counter's People

Contributors

joshwcomeau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-retro-hit-counter's Issues

More legible segments

This component mimics a "seven-segment display": https://en.wikipedia.org/wiki/Seven-segment_display

I chose a simple implementation at first: each segment is exactly the same shape and size, and I'm just rotating them around.

Unfortunately, this means the end result isn't as legible as it could be, especially with thicker segments. Compare this component to another seven-segment display using trapezoids around the edge pieces:

current
alternative

I think it would be good to move towards that alternative version, as the numbers are much easier to read.

Improve build process for working with demo

When working on this component, you can either use the React Storybook stories, or the demo.

The demo is a much nicer dev experience, since you have full access to all the props. However, the demo is its own project, nested within the parent project, and the dependency is a little weird.

The demo installs react-retro-hit-counter from NPM. Then, when building the parent project (the component itself), a separate script copies the dist files into the demo's node_modules directory:

"scripts": {
    "build": "rollup -c && npm run copy-to-demo",
    "copy-to-demo": "rm -rf ./demo/node_modules/react-retro-hit-counter/dist && cp -r ./dist ./demo/node_modules/react-retro-hit-counter/dist",

This is not the "right" way to do this. The only other way that I've seen is to use npm link to create a symlink between the project's dist directory and your global NPM installs, and then another from that global file to the child project's node_modules directory.

When I tried this, though, the demo wasn't rebuilding whenever the files changed. This means I'd have to build the component, wait 3 seconds, make some sort of trivial change to the demo, and wait for the demo to auto-rebuild.

My current solution takes about 3 seconds, and the demo auto-rebuilds. So really, it's not bad... but I feel like there has to be a better way?

Add component tests

Right now, the only tests in this project are for a small helper function that pads strings.

I can think of a few other things that would benefit from testing:

  • Snapshot tests on RetroHitCounter and RetroBorder
  • Segment testing, to ensure that a given number is accurately represented.
  • Tests that updates the values, including interruptions.

a11y support

React Retro Hit Counter creates numbers using SVG segments and a canvas border. This is totally unfriendly to screen readers.

I have a few thoughts for how to make it more accessible:

  • Aria tags
  • Adding zero-opacity text over the hit counter, something like "Hit counter with N hits"?
  • Something else?

I don't really know what would be best. Open to suggestions!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.