Skip to content

Commit

Permalink
Add HOC api and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gerrymi committed May 12, 2020
1 parent 7ccd21f commit 9d532cf
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 69 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ function App() {
```

### Hooks
This is the recommended usage for apps using React 16.8+
```javascript
function HeaderSection() {
const context = {
Expand All @@ -56,9 +55,7 @@ function HeaderSection() {
}
```

## Coming Soon
### HOC
For React < 16.8; Wrap your app with the provider, and access copy through props.
```javascript
function HeaderSection({copy}) {
return (
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/light.min.css">
<title>React App</title>
</head>
<body>
Expand Down
9 changes: 0 additions & 9 deletions src/App.test.tsx

This file was deleted.

53 changes: 0 additions & 53 deletions src/App.tsx

This file was deleted.

7 changes: 7 additions & 0 deletions src/BodySection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";
import { withCopy, CopyType } from "./configureCopy";

const BodySection = ({ copy }: { copy: CopyType }) => {
return <div>{copy.body}</div>;
};
export default withCopy(BodySection);
11 changes: 11 additions & 0 deletions src/Copyful.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ export const createCopyful = <TCopy,>(defaultCopy: TCopy) => {
return useMemo(() => interpolatedCopy, [interpolatedCopy]);
};

const withCopy = (Component: any) => {
const WithCopy = React.forwardRef(function WithCopy(props: any, ref) {
const { innerRef, ...rest } = props;
const copy = useCopy() || defaultCopy;
return <Component copy={copy} ref={innerRef || ref} {...rest} />;
});

return WithCopy;
};

return {
CopyfulProvider,
useCopy,
withCopy,
};
};

Expand Down
15 changes: 15 additions & 0 deletions src/Example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react";
import { CopyfulProvider, getCopySomehow } from "./configureCopy";
import HeaderSection from "./HeaderSection";
import BodySection from "./BodySection";

function App() {
return (
<CopyfulProvider copy={getCopySomehow("en-us")}>
<HeaderSection />{/* Example Using Hooks */}
<BodySection />{/* Example Using HOC */}
</CopyfulProvider>
);
}

export default App;
26 changes: 26 additions & 0 deletions src/HeaderSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from "react";
import { useCopy } from "./configureCopy";

const HeaderSection = () => {
const context = {
someNumber: 4815162342,
someString: "Hello World!",
};
const {
title,
subtitle,
nested: { dynamicValues },
} = useCopy(context).header;

return (
<div className="App">
<header className="App-header">
<h1>{title}</h1>
<h2>{subtitle}</h2>
<p>{dynamicValues}</p>
</header>
</div>
);
}

export default HeaderSection;
25 changes: 25 additions & 0 deletions src/configureCopy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createCopyful } from "./Copyful";
import copy from "./copy.json";

export interface LocalesType {
[key: string]: CopyType;
}
export interface CopyType {
header: {
title: string;
subtitle: string;
nested: {
dynamicValues: string;
};
};
body: string;
}


export const getCopySomehow = (locale: string) => {
return (copy as LocalesType)[locale];
};

export const { CopyfulProvider, useCopy, withCopy } = createCopyful(
getCopySomehow("1337")
);
6 changes: 4 additions & 2 deletions src/copy.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"nested": {
"dynamicValues": "Yes, even dynamic values, like this number: {someNumber} and this string: {someString}."
}
}
},
"body": "Product copy can become difficult to manage across product platforms as well as internal design and engineering tooling. Copy changes frequently and for many reasons (more clarity, typo, legal guidance, etc...) and engineering **must** be involved to make these changes. Sometimes the changes aren't straightforward - it becomes a hunt across repositories or copy has drifted across our web and native applications."
},
"1337": {
"header": {
Expand All @@ -15,6 +16,7 @@
"nested": {
"dynamicValues": "y35, 3v3n dyn4m1c v4lu35, l1k3 7h15 numb3r: {someNumber} 4nd 7h15 57r1n6: {someString}."
}
}
},
"body": "Product copy can become difficult to manage across product platforms as well as internal design and engineering tooling. Copy changes frequently and for many reasons (more clarity, typo, legal guidance, etc...) and engineering **must** be involved to make these changes. Sometimes the changes aren't straightforward - it becomes a hunt across repositories or copy has drifted across our web and native applications."
}
}
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import Example from "./Example";

ReactDOM.render(<App />, document.getElementById("root"));
ReactDOM.render(<Example />, document.getElementById("root"));

0 comments on commit 9d532cf

Please sign in to comment.