Skip to content
This repository was archived by the owner on Feb 14, 2022. It is now read-only.

Commit c631997

Browse files
committed
Add tabs demo
1 parent eedafee commit c631997

File tree

8 files changed

+175
-3
lines changed

8 files changed

+175
-3
lines changed

next.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const withBundleAnalyzer = require('@next/bundle-analyzer')({
33
enabled: process.env.ANALYZE === 'true',
44
})
55
const withSyntaxHighlighting = require('./remark/withSyntaxHighlighting')
6+
const withProse = require('./remark/withProse')
67

78
module.exports = withBundleAnalyzer({
89
pageExtensions: ['js', 'jsx', 'mdx'],
@@ -28,7 +29,7 @@ module.exports = withBundleAnalyzer({
2829
{
2930
loader: '@mdx-js/loader',
3031
options: {
31-
remarkPlugins: [withSyntaxHighlighting],
32+
remarkPlugins: [withProse, withSyntaxHighlighting],
3233
},
3334
},
3435
]

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"build:rss": "node ./.next/server/scripts/build-rss.js"
1010
},
1111
"dependencies": {
12+
"@headlessui/react": "^1.4.0",
1213
"@mdx-js/loader": "^1.6.22",
1314
"@tailwindcss/aspect-ratio": "^0.2.1",
1415
"@tailwindcss/typography": "^0.4.0",

remark/withProse.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const proseComponents = ['Heading']
2+
3+
const isJsNode = (node) => {
4+
return (
5+
['jsx', 'import', 'export'].includes(node.type) &&
6+
!/^<[a-z]+(>|\s)/.test(node.value) &&
7+
!new RegExp(`^<(${proseComponents.join('|')})(>|\\s)`).test(node.value)
8+
)
9+
}
10+
11+
module.exports = function () {
12+
return (tree) => {
13+
let insideProse = false
14+
tree.children = tree.children.flatMap((node, i) => {
15+
if (insideProse && isJsNode(node)) {
16+
insideProse = false
17+
return [{ type: 'jsx', value: '</div>' }, node]
18+
}
19+
if (!insideProse && !isJsNode(node)) {
20+
insideProse = true
21+
return [
22+
{ type: 'jsx', value: '<div className="prose max-w-none">' },
23+
node,
24+
...(i === tree.children.length - 1 ? [{ type: 'jsx', value: '</div>' }] : []),
25+
]
26+
}
27+
if (i === tree.children.length - 1 && insideProse) {
28+
return [node, { type: 'jsx', value: '</div>' }]
29+
}
30+
return [node]
31+
})
32+
}
33+
}

src/components/Post.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export default function Post({ meta, children, posts }) {
181181
</dd>
182182
</dl>
183183
<div className="divide-y divide-gray-200 xl:pb-0 xl:col-span-3 xl:row-span-2">
184-
<div className="prose max-w-none pt-10 pb-8">
184+
<div className="max-w-none pt-10 pb-8">
185185
<MDXProvider>{children}</MDXProvider>
186186
</div>
187187
{meta.footer && (

src/getAllPostPreviews.js

+3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ function dateSortDesc(a, b) {
1212
}
1313

1414
export default function getAllPostPreviews() {
15+
return []
1516
return importAll(require.context('./pages/?preview', true, /\.mdx$/))
1617
.filter((p) => !p.link.includes('/snippets/'))
1718
.filter((p) => p.module.meta.private !== true)
1819
.sort((a, b) => dateSortDesc(a.module.meta.date, b.module.meta.date))
1920
}
2021

2122
export function getAllPosts() {
23+
return []
24+
2225
return importAll(require.context('./pages/?rss', true, /\.mdx$/))
2326
.filter((p) => !p.link.includes('/snippets/'))
2427
.filter((p) => p.module.meta.private !== true)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { useState } from 'react'
2+
import { Tab } from '@headlessui/react'
3+
4+
function classNames(...classes) {
5+
return classes.filter(Boolean).join(' ')
6+
}
7+
8+
export default function Example() {
9+
let [categories] = useState({
10+
Recent: [
11+
{
12+
id: 1,
13+
title: 'Does drinking coffee make you smarter?',
14+
date: '5h ago',
15+
commentCount: 5,
16+
shareCount: 2,
17+
},
18+
{
19+
id: 2,
20+
title: "So you've bought coffee... now what?",
21+
date: '2h ago',
22+
commentCount: 3,
23+
shareCount: 2,
24+
},
25+
],
26+
Popular: [
27+
{
28+
id: 1,
29+
title: 'Is tech making coffee better or worse?',
30+
date: 'Jan 7',
31+
commentCount: 29,
32+
shareCount: 16,
33+
},
34+
{
35+
id: 2,
36+
title: 'The most innovative things happening in coffee',
37+
date: 'Mar 19',
38+
commentCount: 24,
39+
shareCount: 12,
40+
},
41+
],
42+
Trending: [
43+
{
44+
id: 1,
45+
title: 'Ask Me Anything: 10 answers to your questions about coffee',
46+
date: '2d ago',
47+
commentCount: 9,
48+
shareCount: 5,
49+
},
50+
{
51+
id: 2,
52+
title: "The worst advice we've ever heard about coffee",
53+
date: '4d ago',
54+
commentCount: 1,
55+
shareCount: 2,
56+
},
57+
],
58+
})
59+
60+
return (
61+
<div className="my-8 rounded-lg bg-gradient-to-r from-sky-400 to-blue-600 h-[360px]">
62+
<div className="mx-auto w-full max-w-md px-2 py-16 sm:px-0">
63+
<Tab.Group>
64+
<Tab.List className="flex p-1 space-x-1 bg-blue-900/20 rounded-xl">
65+
{Object.keys(categories).map((category) => (
66+
<Tab
67+
key={category}
68+
className={({ selected }) =>
69+
classNames(
70+
'w-full py-2.5 text-sm leading-5 font-medium text-blue-700 rounded-lg',
71+
'focus:outline-none focus:ring-2 ring-offset-2 ring-offset-blue-400 ring-white ring-opacity-60',
72+
selected
73+
? 'bg-white shadow'
74+
: 'text-blue-100 hover:bg-white/[0.12] hover:text-white'
75+
)
76+
}
77+
>
78+
{category}
79+
</Tab>
80+
))}
81+
</Tab.List>
82+
<Tab.Panels className="mt-2">
83+
{Object.values(categories).map((posts, idx) => (
84+
<Tab.Panel
85+
key={idx}
86+
className={classNames(
87+
'bg-white rounded-xl p-3',
88+
'focus:outline-none focus:ring-2 ring-offset-2 ring-offset-blue-400 ring-white ring-opacity-60'
89+
)}
90+
>
91+
<ul>
92+
{posts.map((post) => (
93+
<li
94+
key={post.id}
95+
className="relative p-3 rounded-md hover:bg-gray-100"
96+
>
97+
<h3 className="text-sm font-medium leading-5">
98+
{post.title}
99+
</h3>
100+
101+
<ul className="flex mt-1 space-x-1 text-xs font-normal leading-4 text-gray-500">
102+
<li>{post.date}</li>
103+
<li>&middot;</li>
104+
<li>{post.commentCount} comments</li>
105+
<li>&middot;</li>
106+
<li>{post.shareCount} shares</li>
107+
</ul>
108+
109+
<a
110+
href="#"
111+
className={classNames(
112+
'absolute inset-0 rounded-md',
113+
'focus:z-10 focus:outline-none focus:ring-2 ring-blue-400'
114+
)}
115+
/>
116+
</li>
117+
))}
118+
</ul>
119+
</Tab.Panel>
120+
))}
121+
</Tab.Panels>
122+
</Tab.Group>
123+
</div>
124+
</div>
125+
)
126+
}

src/pages/headless-ui-v1-4/index.mdx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { adamwathan, robinmalfait } from '@/authors'
22
import card from './card.jpg'
3+
import Demo from './components/Demo'
34

45
export const meta = {
56
title: 'Headless UI v1.4: The One With Tabs',
@@ -33,6 +34,8 @@ Earlier this year we started working on [Tailwind UI Ecommerce](https://tailwind
3334

3435
Here's what we ended up with:
3536

37+
<Demo />
38+
3639
```jsx
3740
import { Tab } from '@headlessui/react'
3841

@@ -61,7 +64,7 @@ function MyTabs() {
6164

6265
Like all Headless UI components, this totally abstracts away stuff like keyboard navigation for you so you can create custom tabs in a completely declarative way, without having to think about any of the tricky accessibility details.
6366

64-
[Check out the documentation](https://headlessui.dev/react/tabs) to play with a working example.
67+
[Check out the documentation](https://headlessui.dev/react/tabs) to learn more.
6568

6669
### Closing Disclosures and Popovers
6770

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@
272272
resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.2.0.tgz#f3933a44e365864f4dad5db94158106d511e8131"
273273
integrity sha512-sqKVVVOe5ivCaXDWivIJYVSaEgdQK9ul7a4Kity5Iw7u9+wBAPbX1RMSnLLmp7O4Vzj0WOWwMAJsTL00xwaNug==
274274

275+
"@headlessui/react@^1.4.0":
276+
version "1.4.0"
277+
resolved "https://registry.yarnpkg.com/@headlessui/react/-/react-1.4.0.tgz#c6d424d8ab10ac925e4423d7f3cbab02c30d736a"
278+
integrity sha512-C+FmBVF6YGvqcEI5fa2dfVbEaXr2RGR6Kw1E5HXIISIZEfsrH/yuCgsjWw5nlRF9vbCxmQ/EKs64GAdKeb8gCw==
279+
275280
"@mdx-js/loader@^1.6.22":
276281
version "1.6.22"
277282
resolved "https://registry.yarnpkg.com/@mdx-js/loader/-/loader-1.6.22.tgz#d9e8fe7f8185ff13c9c8639c048b123e30d322c4"

0 commit comments

Comments
 (0)