Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 'fork me' ribbon #861

Merged
merged 7 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ By default, Styleguidist will look for `styleguide.config.js` file in your proje
* [`propsParser`](#propsparser)
* [`require`](#require)
* [`resolver`](#resolver)
* [`ribbon`](#ribbon)
* [`sections`](#sections)
* [`serverHost`](#serverhost)
* [`serverPort`](#serverport)
Expand Down Expand Up @@ -328,6 +329,21 @@ module.exports = {
}
```

#### `ribbon`

Type: `Object`, optional

Shows 'Fork Me' ribbon in the top-right corner. If `ribbon` key is present, then it's required to add `url` property; `text` property is optional. If you want to change styling of the ribbon, please, refer to the [theme section](#theme).

```javascript
module.exports = {
ribbon: {
url: 'http://example.com/',
text: 'Fork me on GitHub',
}
};
```

#### `sections`

Type: `Array`, optional
Expand Down
3 changes: 3 additions & 0 deletions examples/basic/styleguide.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
components: 'src/components/**/[A-Z]*.js',
defaultExample: true,
ribbon: {
url: 'https://github.com/styleguidist/react-styleguidist',
},
webpackConfig: {
module: {
rules: [
Expand Down
1 change: 1 addition & 0 deletions loaders/styleguide-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const CLIENT_CONFIG_OPTIONS = [
'styles',
'compilerConfig',
'editorConfig',
'ribbon',
];

module.exports = function() {};
Expand Down
7 changes: 7 additions & 0 deletions scripts/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ module.exports = {
return annotatedComponents.concat(exportedComponents);
},
},
ribbon: {
type: 'object',
example: {
url: 'http://example.com/',
text: 'Fork me on GitHub',
},
},
sections: {
type: 'array',
default: [],
Expand Down
12 changes: 12 additions & 0 deletions src/rsg-components/Ribbon/Ribbon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import PropTypes from 'prop-types';
import RibbonRenderer from 'rsg-components/Ribbon/RibbonRenderer';

export default function Ribbon({}, { config }) {
const { ribbon } = config;
return ribbon ? <RibbonRenderer {...ribbon} /> : null;
}

Ribbon.contextTypes = {
config: PropTypes.object,
};
36 changes: 36 additions & 0 deletions src/rsg-components/Ribbon/Ribbon.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import Ribbon from './Ribbon';
import { RibbonRenderer, styles } from './RibbonRenderer';

const props = {
classes: classes(styles),
};

describe('Ribbon', () => {
it('should render ribbon if the ribbon is present in the config', () => {
const actual = shallow(<Ribbon />, { context: { config: { ribbon: { url: 'foo.bar' } } } });

expect(actual).toMatchSnapshot();
});

it('should return null if the ribbon is not present in the config', () => {
const actual = shallow(<Ribbon />, { context: { config: {} } });

expect(actual.type()).toBeNull();
});
});
describe('RibbonRenderer', () => {
it('should render ribbon with url', () => {
const actual = shallow(<RibbonRenderer {...props} url="http://example.com" />);

expect(actual).toMatchSnapshot();
});

it('should render ribbon with a text', () => {
const actual = shallow(
<RibbonRenderer {...props} url="http://example.com" text="Share the repo" />
);

expect(actual).toMatchSnapshot();
});
});
54 changes: 54 additions & 0 deletions src/rsg-components/Ribbon/RibbonRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import PropTypes from 'prop-types';
import Styled from 'rsg-components/Styled';

export const styles = ({ color, space, fontSize, fontFamily }) => ({
root: {
position: 'fixed',
top: 0,
right: 0,
width: '149px',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should work without px.

height: '149px',
zIndex: 999,
},
link: {
fontFamily: fontFamily.base,
position: 'relative',
right: -37,
top: -22,
display: 'block',
width: 190,
padding: `${space[0]}px ${space[2]}px`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[[space[0], space[2]]] is a preferred way (note double brackets).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool, thx, should have read more on jss before jumping on styles ;) will fix

textAlign: 'center',
color: color.ribbonText,
fontSize: fontSize.base,
background: color.ribbonBackground,
textDecoration: 'none',
textShadow: '0 -1px 0 rgba(0,0,0,.15)',
transformOrigin: '0 0',
transform: 'rotate(45deg)',
cursor: 'pointer',
},
});

export function RibbonRenderer({ classes, url, text }) {
return (
<div className={classes.root}>
<a href={url} className={classes.link}>
{text}
</a>
</div>
);
}

RibbonRenderer.defaultProps = {
text: 'Fork me on GitHub',
};

RibbonRenderer.propTypes = {
classes: PropTypes.object.isRequired,
url: PropTypes.string.isRequired,
text: PropTypes.string,
};

export default Styled(styles)(RibbonRenderer);
33 changes: 33 additions & 0 deletions src/rsg-components/Ribbon/__snapshots__/Ribbon.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Ribbon should render ribbon if the ribbon is present in the config 1`] = `
<Styled(Ribbon)
url="foo.bar"
/>
`;

exports[`RibbonRenderer should render ribbon with a text 1`] = `
<div
className="root"
>
<a
className="link"
href="http://example.com"
>
Share the repo
</a>
</div>
`;

exports[`RibbonRenderer should render ribbon with url 1`] = `
<div
className="root"
>
<a
className="link"
href="http://example.com"
>
Fork me on GitHub
</a>
</div>
`;
1 change: 1 addition & 0 deletions src/rsg-components/Ribbon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'rsg-components/Ribbon/Ribbon.js';
2 changes: 1 addition & 1 deletion src/rsg-components/StyleGuide/StyleGuide.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('sidebar rendering', () => {
});
});

it('renderer should render logo, table of contents and passed children', () => {
it('renderer should render logo, table of contents, ribbon and passed children', () => {
const actual = shallow(
<StyleGuideRenderer
classes={{}}
Expand Down
2 changes: 2 additions & 0 deletions src/rsg-components/StyleGuide/StyleGuideRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Logo from 'rsg-components/Logo';
import Markdown from 'rsg-components/Markdown';
import Styled from 'rsg-components/Styled';
import cx from 'classnames';
import Ribbon from 'rsg-components/Ribbon';

const styles = ({ color, fontFamily, fontSize, sidebarWidth, mq, space, maxWidth }) => ({
root: {
Expand Down Expand Up @@ -71,6 +72,7 @@ export function StyleGuideRenderer({ classes, title, homepageUrl, children, toc,
{toc}
</div>
)}
<Ribbon />
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renderer should render logo, table of contents and passed children 1`] = `
exports[`renderer should render logo, table of contents, ribbon and passed children 1`] = `
<div
className=""
>
Expand Down Expand Up @@ -47,6 +47,7 @@ exports[`renderer should render logo, table of contents and passed children 1`]
}
/>
</div>
<Ribbon />
</div>
`;

Expand Down
2 changes: 2 additions & 0 deletions src/styles/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const color = {
baseBackground: '#fff',
codeBackground: '#f5f5f5',
sidebarBackground: '#f5f5f5',
ribbonBackground: '#f9970d',
ribbonText: '#fff',
};

export const fontFamily = {
Expand Down