Skip to content

Commit

Permalink
Try to fix bootstrap theme and deploy it!
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 17, 2020
1 parent 7535f54 commit d34d2f7
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"start:v2": "yarn workspace docusaurus-2-website start",
"start:v2:watch": "nodemon --watch \"./packages/*/lib/**/*.*\" --exec \"yarn start:v2\"",
"start:v2:baseUrl": "yarn workspace docusaurus-2-website start:baseUrl",
"start:v2:bootstrap": "yarn workspace docusaurus-2-website start:bootstrap",
"build": "yarn build:packages && yarn build:v2",
"build:packages": "lerna run build --no-private",
"build:v1": "yarn workspace docusaurus-1-website build",
Expand Down
32 changes: 32 additions & 0 deletions packages/docusaurus-theme-bootstrap/src/theme/MDXPage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import Layout from '@theme/Layout';
import {MDXProvider} from '@mdx-js/react';
import MDXComponents from '@theme/MDXComponents';

function MDXPage(props) {
const {content: MDXPageContent} = props;
const {frontMatter, metadata} = MDXPageContent;
const {title, description} = frontMatter;
const {permalink} = metadata;

return (
<Layout title={title} description={description} permalink={permalink}>
<main>
<div className="container margin-vert--lg padding-vert--lg">
<MDXProvider components={MDXComponents}>
<MDXPageContent />
</MDXProvider>
</div>
</main>
</Layout>
);
}

export default MDXPage;
14 changes: 14 additions & 0 deletions packages/docusaurus-theme-bootstrap/src/theme/TabItem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';

function TabItem(props) {
return <div>{props.children}</div>;
}

export default TabItem;
154 changes: 154 additions & 0 deletions packages/docusaurus-theme-bootstrap/src/theme/Tabs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, {useState, Children, useEffect} from 'react';

import clsx from 'clsx';

import styles from './styles.module.css';

const keys = {
left: 37,
right: 39,
tab: 9,
};

// TODO quick fix to make bootstrap theme work
function useUserPreferencesContext() {
const [state, setState] = useState({});

return {
tabGroupChoices: state,
setTabGroupChoices: (id, value) => {
setState((s) => ({...s, [id]: value}));
},
};
}

function Tabs(props) {
const {block, children, defaultValue, values, groupId} = props;

const {tabGroupChoices, setTabGroupChoices} = useUserPreferencesContext();

const [selectedValue, setSelectedValue] = useState(defaultValue);
const [keyboardPress, setKeyboardPress] = useState(false);

if (groupId != null) {
const relevantTabGroupChoice = tabGroupChoices[groupId];
if (
relevantTabGroupChoice != null &&
relevantTabGroupChoice !== selectedValue &&
values.some((value) => value.value === relevantTabGroupChoice)
) {
setSelectedValue(relevantTabGroupChoice);
}
}

const changeSelectedValue = (newValue) => {
setSelectedValue(newValue);
if (groupId != null) {
setTabGroupChoices(groupId, newValue);
}
};

const tabRefs = [];

const focusNextTab = (tabs, target) => {
const next = tabs.indexOf(target) + 1;

if (!tabs[next]) {
tabs[0].focus();
} else {
tabs[next].focus();
}
};

const focusPreviousTab = (tabs, target) => {
const prev = tabs.indexOf(target) - 1;

if (!tabs[prev]) {
tabs[tabs.length - 1].focus();
} else {
tabs[prev].focus();
}
};

const handleKeydown = (tabs, target, event) => {
switch (event.keyCode) {
case keys.right:
focusNextTab(tabs, target);
break;
case keys.left:
focusPreviousTab(tabs, target);
break;
default:
break;
}
};

const handleKeyboardEvent = (event) => {
if (event.metaKey || event.altKey || event.ctrlKey) {
return;
}

setKeyboardPress(true);
};

const handleMouseEvent = () => {
setKeyboardPress(false);
};

useEffect(() => {
window.addEventListener('keydown', handleKeyboardEvent);
window.addEventListener('mousedown', handleMouseEvent);
}, []);

return (
<div>
<ul
role="tablist"
aria-orientation="horizontal"
className={clsx('tabs', {
'tabs--block': block,
})}>
{values.map(({value, label}) => (
<li
role="tab"
tabIndex={0}
aria-selected={selectedValue === value}
className={clsx('tabs__item', styles.tabItem, {
'tabs__item--active': selectedValue === value,
})}
style={keyboardPress ? {} : {outline: 'none'}}
key={value}
ref={(tabControl) => tabRefs.push(tabControl)}
onKeyDown={(event) => {
handleKeydown(tabRefs, event.target, event);
handleKeyboardEvent(event);
}}
onFocus={() => changeSelectedValue(value)}
onClick={() => {
changeSelectedValue(value);
setKeyboardPress(false);
}}
onPointerDown={() => setKeyboardPress(false)}>
{label}
</li>
))}
</ul>
<div role="tabpanel" className="margin-vert--md">
{
Children.toArray(children).filter(
(child) => child.props.value === selectedValue,
)[0]
}
</div>
</div>
);
}

export default Tabs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.tabItem {
margin-top: 0 !important;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import defaultTheme from 'prism-react-renderer/themes/palenight';

const usePrismTheme = () => {
return defaultTheme;
};

export default usePrismTheme;
14 changes: 11 additions & 3 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const allDocHomesPaths = [
];

const baseUrl = process.env.BASE_URL || '/';
const isBootstrapPreset = process.env.DOCUSAURUS_PRESET === 'bootstrap';
const isVersioningDisabled = !!process.env.DISABLE_VERSIONING;

if (isBootstrapPreset) {
console.log('Will use bootstrap preset!');
}

module.exports = {
title: 'Docusaurus',
Expand All @@ -23,7 +29,7 @@ module.exports = {
projectName: 'docusaurus',
baseUrl,
url: 'https://v2.docusaurus.io',
onBrokenLinks: 'throw',
onBrokenLinks: isVersioningDisabled ? 'warn' : 'throw',
favicon: 'img/docusaurus.ico',
customFields: {
description:
Expand Down Expand Up @@ -154,7 +160,9 @@ module.exports = {
],
presets: [
[
'@docusaurus/preset-classic',
isBootstrapPreset
? '@docusaurus/preset-bootstrap'
: '@docusaurus/preset-classic',
{
debug: true, // force debug plugin usage
docs: {
Expand All @@ -166,7 +174,7 @@ module.exports = {
showLastUpdateAuthor: true,
showLastUpdateTime: true,
remarkPlugins: [require('./src/plugins/remark-npm2yarn')],
disableVersioning: !!process.env.DISABLE_VERSIONING,
disableVersioning: isVersioningDisabled,
},
blog: {
// routeBasePath: '/',
Expand Down
10 changes: 7 additions & 3 deletions website/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
# workaroud: a _redirect file is created in website/netlifyDeploy
# can't we have context-based redirects with Netlify? :'(
[[context.deploy-preview.redirects]]
from = "/build/*"
to = "/build/404.html"
from = "/classic/*"
to = "/classic/404.html"
status = 200
[[context.deploy-preview.redirects]]
from = "/bootstrap/*"
to = "/bootstrap/404.html"
status = 200
[[context.deploy-preview.redirects]]
from = "/*"
to = "/build/"
to = "/classic/"

9 changes: 6 additions & 3 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
"deploy": "docusaurus deploy",
"start:baseUrl": "BASE_URL='/build/' yarn start",
"build:baseUrl": "BASE_URL='/build/' yarn build",
"start:bootstrap": "DOCUSAURUS_PRESET=bootstrap yarn start",
"build:bootstrap": "DOCUSAURUS_PRESET=bootstrap yarn build",
"netlify:build:production": "yarn build",
"netlify:build:deployPreview": "yarn build:baseUrl && yarn netlify:build:deployPreview:moveBuild && yarn netlify:build:deployPreview:redirects",
"netlify:build:deployPreview:moveBuild": "yarn rimraf netlifyDeploy && mkdir netlifyDeploy && mv build netlifyDeploy",
"netlify:build:deployPreview:redirects": "echo 'Writing Netlify baseUrl deployPreview _redirects file' && echo '/build/* /build/404.html 200' >> netlifyDeploy/_redirects && echo '/* /build/' >> netlifyDeploy/_redirects",
"netlify:build:deployPreview": "yarn rimraf netlifyDeploy && yarn netlify:build:deployPreview:classic && yarn netlify:build:deployPreview:bootstrap && yarn netlify:build:deployPreview:moveBuild && yarn netlify:build:deployPreview:redirects",
"netlify:build:deployPreview:classic": "BASE_URL='/classic/' yarn build --out-dir netlifyDeploy/classic",
"netlify:build:deployPreview:bootstrap": "BASE_URL='/bootstrap/' DOCUSAURUS_PRESET=bootstrap DISABLE_VERSIONING=true yarn build --out-dir netlifyDeploy/bootstrap",
"netlify:build:deployPreview:redirects": "echo 'Writing Netlify baseUrl deployPreview _redirects file' && echo '/classic/* /classic/404.html 200' >> netlifyDeploy/_redirects && echo '/bootstrap/* /bootstrap/404.html 200' >> netlifyDeploy/_redirects && echo '/* /classic/' >> netlifyDeploy/_redirects",
"netlify:test": "yarn netlify:build:deployPreview && yarn netlify dev --debug"
},
"dependencies": {
Expand Down

0 comments on commit d34d2f7

Please sign in to comment.