Skip to content

Feat: Forkme ribbon option #647

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 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 @@ -323,6 +324,23 @@ 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`, `color`, and `background` are optional.

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

#### `sections`

Type: `Array`, optional
Expand Down
1 change: 1 addition & 0 deletions loaders/styleguide-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const CLIENT_CONFIG_OPTIONS = [
'theme',
'styles',
'compilerConfig',
'ribbon',
];

module.exports = function() {};
Expand Down
9 changes: 9 additions & 0 deletions scripts/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ module.exports = {
type: 'function',
default: reactDocgen.resolver.findAllExportedComponentDefinitions,
},
ribbon: {
type: 'object',
example: {
url: 'http://example.com/',
text: 'Fork me on GitHub',
color: '#fff',
background: '#24292e',
},
},
sections: {
type: 'array',
default: [],
Expand Down
11 changes: 11 additions & 0 deletions src/rsg-components/Ribbon/Ribbon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
Copy link
Member

Choose a reason for hiding this comment

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

Please take a look at https://react-styleguidist.js.org/docs/development.html This component should be split into two: Ribbon that will read config for a React context and RibbonRenderer — this component that prints actual HTML.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, please take a look now and see if it's correct now.

import PropTypes from 'prop-types';
import RibbonRenderer from 'rsg-components/Ribbon/RibbonRenderer';

export default function Ribbon({}, { config }) {
return <RibbonRenderer {...config.ribbon} />;
}

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

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

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();
});

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

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

export const styles = ({ color, fontSize, fontFamily }) => ({
root: {
position: 'fixed',
top: 0,
right: 0,
width: '149px',
height: '149px',
zIndex: 999,
},
link: {
fontFamily: fontFamily.base,
position: 'relative',
right: -37,
top: -22,
display: 'block',
width: 190,
padding: '5px 15px',
textAlign: 'center',
color: 'white',
fontSize: fontSize.base,
background: color.ribbonBackground,
textDecoration: 'none',
textShadow: '0 -1px 0 rgba(0,0,0,.15)',
transformOrigin: '0 0',
transform: 'rotate(45deg)',
},
});

export function RibbonRenderer({ classes, url, text, color, background }) {
return (
<div className={classes.root}>
<a href={url} className={classes.link} style={{ color, background }}>
{text || 'Fork me on GitHub'}
</a>
</div>
);
}

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

export default Styled(styles)(RibbonRenderer);
58 changes: 58 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,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should render ribbon with a text 1`] = `
<div
className="root"
>
<a
className="link"
href="http://example.com"
style={
Object {
"background": undefined,
"color": undefined,
}
}
>
Share the repo
</a>
</div>
`;

exports[`should render ribbon with custom background and color 1`] = `
<div
className="root"
>
<a
className="link"
href="http://example.com"
style={
Object {
"background": "#cdcdcd",
"color": "#aa00bb",
}
}
>
Share the repo
</a>
</div>
`;

exports[`should render ribbon with url 1`] = `
<div
className="root"
>
<a
className="link"
href="http://example.com"
style={
Object {
"background": undefined,
"color": undefined,
}
}
>
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 './Ribbon.js';
1 change: 1 addition & 0 deletions src/rsg-components/StyleGuide/StyleGuide.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default class StyleGuide extends Component {
homepageUrl={HOMEPAGE}
toc={<TableOfContents sections={sections} />}
hasSidebar={config.showSidebar && !isolatedComponent}
ribbon={config.ribbon}
Copy link
Member

Choose a reason for hiding this comment

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

You don’t need this because all config is accessible in the context.

Copy link
Author

Choose a reason for hiding this comment

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

I think we still need this here as we're passing it down to the component to check if we need to render a ribbon (just like hasSidebar).

Copy link
Member

Choose a reason for hiding this comment

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

No if you’ll read config from the context as I’m suggesting ;-)

>
<Sections sections={sections} depth={1} />
</StyleGuideRenderer>
Expand Down
13 changes: 12 additions & 1 deletion 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 '../Ribbon';

const styles = ({ color, fontFamily, fontSize, sidebarWidth, mq, space, maxWidth }) => ({
root: {
Expand Down Expand Up @@ -54,7 +55,15 @@ const styles = ({ color, fontFamily, fontSize, sidebarWidth, mq, space, maxWidth
},
});

export function StyleGuideRenderer({ classes, title, homepageUrl, children, toc, hasSidebar }) {
export function StyleGuideRenderer({
classes,
title,
homepageUrl,
children,
toc,
hasSidebar,
ribbon,
}) {
return (
<div className={cx(classes.root, hasSidebar && classes.hasSidebar)}>
<main className={classes.content}>
Expand All @@ -71,6 +80,7 @@ export function StyleGuideRenderer({ classes, title, homepageUrl, children, toc,
{toc}
</div>
)}
{ribbon && <Ribbon />}
</div>
);
}
Expand All @@ -82,6 +92,7 @@ StyleGuideRenderer.propTypes = {
children: PropTypes.node.isRequired,
toc: PropTypes.node.isRequired,
hasSidebar: PropTypes.bool,
ribbon: PropTypes.object,
};

export default Styled(styles)(StyleGuideRenderer);
1 change: 1 addition & 0 deletions src/styles/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const color = {
errorBackground: '#c00',
codeBackground: '#f5f5f5',
sidebarBackground: '#f5f5f5',
ribbonBackground: '#f9970d',
};

export const fontFamily = {
Expand Down