Skip to content

Commit

Permalink
feat: Add PseudoClassesComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
myxvisual committed Aug 8, 2017
1 parent 3140e01 commit 61f9f48
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ElementState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export default class ElementState extends React.Component<ElementStateProps, {}>
};

static contextTypes = { theme: PropTypes.object };

context: { theme: ReactUWP.ThemeType };

rootElm: HTMLElement;
originStyle: CSSStyleDeclaration;
visitedStyle: React.CSSProperties = {};
Expand Down
43 changes: 43 additions & 0 deletions src/PseudoClassesComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as React from "react";
import * as PropTypes from "prop-types";

import ElementState, { ElementStateProps } from "./ElementState";
import spreadObject from "./common/spreadObject";

const pseudoClassesNames = ["&:hover", "&:active", "&:visited", "&:focus", "&:disabled"];

export class PseudoClassesComponent extends React.Component<any> {
static contextTypes = { theme: PropTypes.object };
context: { theme: ReactUWP.ThemeType };

render () {
const { style, children, ...attributes } = this.props;

if (style) {
const { primaryObject, secondaryObject } = spreadObject(this.props, pseudoClassesNames);
return (
<ElementState
{...attributes}
style={primaryObject}
{...{
hoverStyle: secondaryObject["&:hover"],
activeStyle: secondaryObject["&:active"],
visitedStyle: secondaryObject["&:visited"],
focusStyle: secondaryObject["&:focus"],
disabledStyle: secondaryObject["&:disabled"]
}}
>
{children}
</ElementState>
);
} else {
return (
<ElementState {...attributes}>
{children}
</ElementState>
);
}
}
}

export default PseudoClassesComponent;
4 changes: 3 additions & 1 deletion src/common/spreadObject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function spreadObject(obj: any, keys: string[]) {
export function spreadObject(obj: any, keys: string[]) {
const primaryObject: any = {};
const secondaryObject: any = {};
const canCheckObjectSymbol = obj !== null && typeof Object.getOwnPropertySymbols === "function";
Expand All @@ -24,3 +24,5 @@ function spreadObject(obj: any, keys: string[]) {

return { primaryObject, secondaryObject };
}

export default spreadObject;

0 comments on commit 61f9f48

Please sign in to comment.