badgen.net/components/builder-preview.tsx

73 wiersze
1.8 KiB
TypeScript
Czysty Zwykły widok Historia

import debounceRender from 'react-debounce-render'
2019-10-04 12:45:52 +00:00
import BadgenTitle from './badgen-title'
2018-09-13 03:25:34 +00:00
const BadgePreview = ({ host, badgeURL, focus }) => {
const showPreview = focus || !!badgeURL
2018-09-06 09:54:28 +00:00
return (
2018-10-26 07:19:09 +00:00
<div className='header-preview'>
<div className={'title ' + (showPreview ? 'hidden' : 'show')}>
2019-06-02 06:11:51 +00:00
<BadgenTitle host={host} />
2018-09-20 06:38:02 +00:00
</div>
<div className={'preview ' + (showPreview ? 'show' : 'none')}>
2018-09-20 06:38:02 +00:00
<PreviewBadge host={host} url={badgeURL} />
</div>
2018-09-06 09:54:28 +00:00
<style jsx>{`
2018-10-26 07:19:09 +00:00
.header-preview {
2018-09-20 06:38:02 +00:00
height: calc(50vh - 100px);
width: 100%;
2018-10-26 07:19:09 +00:00
min-height: 160px;
2018-09-20 06:38:02 +00:00
position: relative;
}
.title, .preview {
position: absolute;
width: 100%;
height: 100%;
2018-09-13 06:21:46 +00:00
display: flex;
2018-09-20 06:38:02 +00:00
flex-direction: column;
justify-content: center;
2018-09-13 06:21:46 +00:00
align-items: center;
2018-09-06 09:54:28 +00:00
}
2018-10-19 05:06:41 +00:00
.title {
transition: all 200ms ease-out;
}
.title.hidden {
2018-09-20 06:38:02 +00:00
opacity: 0;
transform: translateY(-20px);
2018-10-19 05:06:41 +00:00
}
.title.show {
transition-delay: 100ms;
2018-09-20 06:38:02 +00:00
}
.preview {
pointer-events: none;
opacity: 0;
transform: translateY(30px);
transition: all 200ms cubic-bezier(0.215, 0.61, 0.355, 1);
}
.preview.show {
2018-09-20 06:38:02 +00:00
opacity: 1;
transform: translateY(0);
2018-10-19 05:06:41 +00:00
transition-delay: 180ms;
2018-09-07 07:13:08 +00:00
}
2019-08-24 08:24:46 +00:00
`}
</style>
2018-09-06 09:54:28 +00:00
</div>
)
}
2018-09-07 07:13:08 +00:00
2018-09-20 06:38:02 +00:00
const PreviewBadge = debounceRender(({ host, url }) => {
return <img style={{ height: '30px' }} src={genBadgeSrc(host, url)} />
2019-06-02 06:11:51 +00:00
}, 300)
2018-09-20 06:38:02 +00:00
2018-09-08 14:40:15 +00:00
const genBadgeSrc = (host, url) => {
2019-06-02 06:11:51 +00:00
if (!url) {
2019-06-02 14:01:41 +00:00
return host + 'badge/%20/%20'
2018-09-08 14:40:15 +00:00
}
if (url.split('/').length > 2) {
return host + url
2018-09-07 07:13:08 +00:00
} else {
2019-06-02 14:01:41 +00:00
return host + 'badge/%20/%20'
2018-09-07 07:13:08 +00:00
}
}
2018-09-13 03:25:34 +00:00
2018-09-20 06:38:02 +00:00
export default BadgePreview