Skip to content

Commit

Permalink
🎨 Chakra tabs #2679
Browse files Browse the repository at this point in the history
  • Loading branch information
millianapia committed Nov 15, 2024
1 parent 1f9b518 commit 6489024
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 107 deletions.
71 changes: 11 additions & 60 deletions web/components/src/Tabs/Tab.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,15 @@
import { forwardRef, CSSProperties } from 'react'
import styled from 'styled-components'
import { outlineTemplate, Tokens } from '@utils'
import { Tab as CTab, TabProps as CTabProps } from '@chakra-ui/react'
import { forwardRef } from 'react';
import * as RadixTabs from '@radix-ui/react-tabs';

const { outline } = Tokens
export type TabProps = RadixTabs.TabsTriggerProps;

const StyledTab = styled(CTab)`
color: var(--font-color);
background: transparent;
border: none;
padding: var(--space-xSmall) 0;
/* Not sure about this one, but some spaces for tab components that wrap multiple lines */
margin-bottom: var(--space-small);
:not(:last-child) {
margin-right: var(--space-medium);
}
&:hover {
cursor: pointer;
}
color: var(--color-on-background);
&:active {
background: transparent;
}
&:focus-visible {
outline: none;
${outlineTemplate(outline)}
outline-color: var(--mist-blue-100);
}
&[data-selected] {
border-bottom: 2px solid;
}
`

export type TabProps = CTabProps & {
variant?: string
}

export const Tab = forwardRef<HTMLButtonElement, TabProps>(function CTab({ children, variant = 'line', ...rest }, ref) {
export const Tab = forwardRef<HTMLButtonElement, TabProps>(function Tab(
{ children, ...rest },
ref
) {
return (
<StyledTab
ref={ref}
_selected={{
'--font-color': 'var(--color-on-background)',
borderColor: 'var(--color-on-background)',
borderBottom: '2px solid',
}}
variant={variant}
{...rest}
style={
{
'--font-color': 'var(--color-on-background)',
} as CSSProperties
}
>
<RadixTabs.Trigger ref={ref} {...rest}>
{children}
</StyledTab>
)
})
</RadixTabs.Trigger>
);
});
19 changes: 11 additions & 8 deletions web/components/src/Tabs/TabList.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { forwardRef } from 'react'
import { TabList as CTabList, TabListProps as CTabListProps } from '@chakra-ui/react'
import { forwardRef } from 'react';
import * as RadixTabs from '@radix-ui/react-tabs';

export type TabListProps = CTabListProps
export type TabListProps = RadixTabs.TabsListProps;

export const TabList = forwardRef<HTMLDivElement, TabListProps>(function TabList({ children, ...rest }, ref) {
export const TabList = forwardRef<HTMLDivElement, TabListProps>(function TabList(
{ children, ...rest },
ref
) {
return (
<CTabList ref={ref} {...rest} style={{ border: 'none', flexWrap: 'wrap' }}>
<RadixTabs.List ref={ref} {...rest}>
{children}
</CTabList>
)
})
</RadixTabs.List>
);
});
19 changes: 11 additions & 8 deletions web/components/src/Tabs/TabPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { forwardRef } from 'react'
import { TabPanel as CTabPanel, TabPanelProps as CTabPanelProps } from '@chakra-ui/react'
import { forwardRef } from 'react';
import * as RadixTabs from '@radix-ui/react-tabs';

export type TabPanelProps = CTabPanelProps
export type TabPanelProps = RadixTabs.TabsContentProps;

export const TabPanel = forwardRef<HTMLDivElement, TabPanelProps>(function TabPanel({ children, ...rest }, ref) {
export const TabPanel = forwardRef<HTMLDivElement, TabPanelProps>(function TabPanel(
{ children, ...rest },
ref
) {
return (
<CTabPanel ref={ref} {...rest}>
<RadixTabs.Content ref={ref} {...rest}>
{children}
</CTabPanel>
)
})
</RadixTabs.Content>
);
});
19 changes: 11 additions & 8 deletions web/components/src/Tabs/TabPanels.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { forwardRef } from 'react'
import { TabPanels as CTabPanels, TabPanelsProps as CTabPanelsProps } from '@chakra-ui/react'
import { forwardRef } from 'react';
import * as RadixTabs from '@radix-ui/react-tabs';

export type TabPanelsProps = CTabPanelsProps
export type TabPanelsProps = { children: React.ReactNode };

export const TabPanels = forwardRef<HTMLDivElement, TabPanelsProps>(function TabPanels({ children, ...rest }, ref) {
export const TabPanels = forwardRef<HTMLDivElement, TabPanelsProps>(function TabPanels(
{ children, ...rest },
ref
) {
return (
<CTabPanels ref={ref} {...rest}>
<div ref={ref} {...rest}>
{children}
</CTabPanels>
)
})
</div>
);
});
19 changes: 11 additions & 8 deletions web/components/src/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { forwardRef } from 'react'
import { Tabs as CTabs, TabsProps as CTabsProps } from '@chakra-ui/react'
import { forwardRef } from 'react';
import * as RadixTabs from '@radix-ui/react-tabs';

export type TabsProps = CTabsProps
export type TabsProps = RadixTabs.TabsProps;

export const Tabs = forwardRef<HTMLDivElement, TabsProps>(function Tabs({ children, ...rest }, ref) {
export const Tabs = forwardRef<HTMLDivElement, TabsProps>(function Tabs(
{ children, ...rest },
ref
) {
return (
<CTabs ref={ref} {...rest}>
<RadixTabs.Root ref={ref} {...rest}>
{children}
</CTabs>
)
})
</RadixTabs.Root>
);
});
9 changes: 5 additions & 4 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
"@portabletext/react": "^3.1.0",
"@portabletext/to-html": "^2.0.13",
"@portabletext/types": "^2.0.13",
"@radix-ui/react-accordion": "^1.2.0",
"@radix-ui/react-tabs": "^1.1.1",
"@react-spring/web": "^9.4.5",
"@sanity/asset-utils": "^1.3.0",
"@sanity/client": "5.4.2",
"@sanity/image-url": "^1.0.2",
"@sanity/webhook": "^2.0.0",
"@radix-ui/react-accordion": "^1.2.0",
"algoliasearch": "^4.16.0",
"date-fns": "^2.29.3",
"date-fns-tz": "^2.0.0",
Expand Down Expand Up @@ -84,19 +85,19 @@
"@tailwindcss/typography": "^0.5.10",
"@testing-library/jest-dom": "^6.1.4",
"@testing-library/react": "^14.0.0",
"@types/easy-soap-request": "^4.1.1",
"@types/http-proxy": "^1.17.10",
"@types/jest": "^29.5.7",
"@types/node": "^18.14.9999",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/react-instantsearch": "^6.10.1",
"@types/testing-library__jest-dom": "^5.14.5",
"@types/easy-soap-request": "^4.1.1",
"@types/react-transition-group": "^4.4.10",
"@types/styled-components": "^5.1.26",
"@types/testing-library__jest-dom": "^5.14.5",
"@types/uuid": "^8.3.4",
"@types/video.js": "^7.3.57",
"@types/xml2js": "^0.4.11",
"@types/react-transition-group": "^4.4.10",
"autoprefixer": "^10.4.17",
"babel-plugin-styled-components": "^2.1.1",
"jest": "^28.1.0",
Expand Down
14 changes: 3 additions & 11 deletions web/pageComponents/search/SearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ const SearchResults = (props: SearchResultsProps) => {
}
}, [userClicked, scopedResults, options, results?.__isArtificial, indexUiState.query, currentRefinement, refine])

const handleTabChange = (index: number) => {
setUserClicked(true)
const activeIndex = options[index]
if (activeIndex) {
refine(activeIndex.value)
}
}

const getHitProps = (tab: string) => {
switch (tab) {
case 'Magazine':
Expand All @@ -79,10 +71,10 @@ const SearchResults = (props: SearchResultsProps) => {
<>
{hasQuery && (
<Results ref={resultsRef}>
<Tabs index={activeTab || 0} onChange={handleTabChange}>
<Tabs >
<TabList>
{options.map((item) => (
<Tab key={item.label}>
<Tab key={item.label} value={item.label}>
<TabText>
{item.label}
<HitsContainer>
Expand All @@ -99,7 +91,7 @@ const SearchResults = (props: SearchResultsProps) => {
</TabList>
<TabPanels>
{options.map((it) => (
<TabPanel key={it.label}>
<TabPanel key={it.label} value={it.label}>
<TotalResultsStat hitsPerPage={HITS_PER_PAGE} />
<Hits hitComponent={getHitProps(it.label)} />
</TabPanel>
Expand Down
92 changes: 92 additions & 0 deletions web/pnpm-lock.yaml

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

0 comments on commit 6489024

Please sign in to comment.