Skip to content

Commit

Permalink
CardTile: Add skeleton loading
Browse files Browse the repository at this point in the history
Can be enabled by passing null to id, name or cost prop.
  • Loading branch information
beheh committed Nov 16, 2017
1 parent 1ab6c9f commit d88023b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added CardTile skeleton for null props

### Changed
- Upgraded dependencies

Expand Down
37 changes: 29 additions & 8 deletions src/components/CardTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as React from "react";
import styled, { injectGlobal } from "styled-components";

export interface CardTileProps extends React.ClassAttributes<CardTile> {
id: string;
name: string;
cost: number;
id: string | null;
name: string | null;
cost: number | null;
icon?: string;
rarity?: string;
disabled?: boolean;
Expand Down Expand Up @@ -106,15 +106,17 @@ const CardTileNameBase = CardTileTextElement.extend`
`;

const CardTileName = CardTileNameBase.extend`
background-image: linear-gradient(
background: linear-gradient(
65deg,
#313109,
#313131 calc(100% - 96px),
rgba(49, 49, 49, 0) calc(100% - 26px),
rgba(49, 49, 49, 0)
),
${(props: React.HTMLProps<HTMLDivElement> & { cardId: string }) =>
`url("https://art.hearthstonejson.com/v1/tiles/${props.cardId}.png")`};
${(props: React.HTMLProps<HTMLDivElement> & { cardId: string | null }) =>
props.cardId !== null
? `url("https://art.hearthstonejson.com/v1/tiles/${props.cardId}.png")`
: "rgba(255, 255, 255, 0.3)"};
`;

const CardTileGem = CardTileTextElement.extend`
Expand Down Expand Up @@ -158,8 +160,18 @@ const CardTileCounter = (CardTileTextElement as any).extend`
border-left: solid 1px black;
`;

const SkeletonLine = (styled.span as any)`
display: inline-block;
background-color: rgba(255, 255, 255, 0.3);
border-radius: 1em;
height: 0.75em;
width: ${(props: React.HTMLProps<HTMLSpanElement> & { width?: string }) =>
props.width || 0};
`;

export interface CardTileState {
flashIndex: number;
skeletonNameWidth?: string;
}

export default class CardTile extends React.Component<
Expand All @@ -170,6 +182,7 @@ export default class CardTile extends React.Component<
super(props, context);
this.state = {
flashIndex: 0,
skeletonNameWidth: `${35 + Math.round(Math.random() * 35)}%`,
};
}

Expand All @@ -190,7 +203,11 @@ export default class CardTile extends React.Component<
return (
<CardTileName cardId={this.props.id}>
{this.props.icon ? <img src={this.props.icon} /> : null}
{this.props.name}
{this.props.name !== null ? (
this.props.name
) : (
<SkeletonLine width={this.state.skeletonNameWidth} />
)}
</CardTileName>
);
}
Expand Down Expand Up @@ -223,7 +240,11 @@ export default class CardTile extends React.Component<
<CardTileGem
rarity={this.props.showRarity ? this.props.rarity : undefined}
>
{this.props.cost}
{this.props.cost !== null ? (
this.props.cost
) : (
<SkeletonLine width={"0.75em"} />
)}
</CardTileGem>
{this.renderName()}
{this.renderCount()}
Expand Down

0 comments on commit d88023b

Please sign in to comment.