Skip to content

Commit

Permalink
refactor(@tabnews/ui): add lib to share UI code
Browse files Browse the repository at this point in the history
  • Loading branch information
aprendendofelipe committed Oct 2, 2024
1 parent a67be7f commit a65c71c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 113 deletions.
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module.exports = {
compiler: {
styledComponents: true,
},
i18n: {
locales: ['pt-BR'],
defaultLocale: 'pt-BR',
},
redirects() {
return [
{
Expand Down
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"@bytemd/plugin-mermaid": "1.21.0",
"@bytemd/react": "1.21.0",
"@primer/octicons-react": "19.11.0",
"@primer/react": "36.27.0",
"@react-email/components": "0.0.19",
"@react-email/render": "0.0.15",
"@resvg/resvg-js": "2.6.2",
"@tabnews/ui": "0.0.3",
"@upstash/ratelimit": "1.2.1",
"@upstash/redis": "1.31.6",
"@vercel/analytics": "1.3.1",
Expand Down Expand Up @@ -43,7 +43,6 @@
"satori": "0.10.13",
"slug": "9.1.0",
"snakeize": "0.1.0",
"styled-components": "5.3.11",
"swr": "2.2.5",
"vis-network": "9.1.9"
},
Expand Down
49 changes: 1 addition & 48 deletions pages/_document.public.js
Original file line number Diff line number Diff line change
@@ -1,48 +1 @@
import Document, { Head, Html, Main, NextScript } from 'next/document';
import Script from 'next/script';
import { ServerStyleSheet } from 'styled-components';

const noFlashScript = `if (['auto','night'].includes(localStorage.getItem('colorMode')))
document.documentElement.setAttribute('data-no-flash', true)`;

export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;

try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
});

const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
};
} finally {
sheet.seal();
}
}

render() {
return (
<Html lang="pt-br">
<Head />
<body>
<Script id="theme" strategy="beforeInteractive">
{noFlashScript}
</Script>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export { Document as default } from '@tabnews/ui/document';
3 changes: 2 additions & 1 deletion pages/interface/components/SkeletonLoader/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useTheme } from '@primer/react';
import ContentLoader from 'react-content-loader';

import { useTheme } from '@/TabNewsUI';

/**
* @param {import('react-content-loader').IContentLoaderProps} props
*/
Expand Down
19 changes: 9 additions & 10 deletions pages/interface/components/TabNewsUI/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export { default as Header } from '@/Header';
export { HeaderLink, Link, NavItem, default as NextLink, TabNavLink } from '@/Link';
export { Editor, default as Viewer } from '@/Markdown';
export { EditorColors, EditorStyles, ViewerStyles } from '@/Markdown/styles';
export { default as Pagination } from '@/Pagination';
export { default as PasswordInput } from '@/PasswordInput';
export { default as PastTime } from '@/PastTime';
export { default as Pagination } from '@/Pagination';
export { default as NextNProgress } from '@/Progressbar';
export { default as ReadTime } from '@/ReadTime';
export { default as RecentTabNav } from '@/RecentTabNav';
Expand All @@ -33,7 +33,7 @@ export {
ActionList,
ActionMenu,
AnchoredOverlay,
BaseStyles,
AutoThemeProvider,
Box,
BranchName,
Button,
Expand All @@ -47,18 +47,17 @@ export {
LabelGroup,
NavList,
Overlay,
Pagehead,
Header as PrimerHeader,
Link as PrimerLink,
TabNav as PrimerTabNav,
ThemeProvider as PrimerThemeProvider,
Tooltip as PrimerTooltip,
SSRProvider,
SegmentedControl,
Spinner,
Text,
TextInput,
Truncate,
useConfirm,
useTheme,
} from '@primer/react';
} from '@tabnews/ui';

import { primer } from '@tabnews/ui';

const { Header: PrimerHeader, Link: PrimerLink, TabNav: PrimerTabNav, Tooltip: PrimerTooltip } = primer;

export { PrimerHeader, PrimerLink, PrimerTabNav, PrimerTooltip };
57 changes: 7 additions & 50 deletions pages/interface/components/ThemeProvider/index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,11 @@
import { useEffect, useLayoutEffect, useState } from 'react';
import { AutoThemeProvider, NextNProgress, ViewerStyles } from '@/TabNewsUI';

import { BaseStyles, NextNProgress, PrimerThemeProvider, SSRProvider, useTheme, ViewerStyles } from '@/TabNewsUI';

// script to be called before interactive in _document.js
// if (['auto','night'].includes(localStorage.getItem('colorMode')))
// document.documentElement.setAttribute('data-no-flash', true)

const removeNoFlashStyle = () => setTimeout(() => document.documentElement.removeAttribute('data-no-flash'));
const useBrowserLayoutEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect;

export default function ThemeProvider({ children, defaultColorMode, ...props }) {
const [colorMode, setColorMode] = useState(defaultColorMode === 'night' ? 'night' : 'day');

useBrowserLayoutEffect(() => {
const cachedColorMode = localStorage.getItem('colorMode') || colorMode;
if (cachedColorMode == colorMode) return;
setColorMode(cachedColorMode);
removeNoFlashStyle();
}, []);

return (
<SSRProvider>
<PrimerThemeProvider colorMode={colorMode} {...props}>
<BaseStyles>
<NextNProgress options={{ showSpinner: false }} />
<NoFleshGlobalStyle />
{children}
<ViewerStyles />
</BaseStyles>
</PrimerThemeProvider>
</SSRProvider>
);
}

function NoFleshGlobalStyle() {
const {
resolvedColorScheme,
theme: { colors },
} = useTheme();
export default function ThemeProvider({ children, ...props }) {
return (
<style jsx global>{`
html[data-no-flash='true']:root {
visibility: hidden;
}
html:root {
color-scheme: ${resolvedColorScheme};
}
body {
background: ${colors.canvas.default};
}
`}</style>
<AutoThemeProvider {...props}>
<NextNProgress options={{ showSpinner: false }} />
{children}
<ViewerStyles />
</AutoThemeProvider>
);
}

0 comments on commit a65c71c

Please sign in to comment.