This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cart i2: Toolbar based view switcher (#4811)
* New icons * Add new view switcher * Context to pass down component controls * Implement switcher
- Loading branch information
1 parent
df51e2f
commit 410eb43
Showing
10 changed files
with
173 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { createContext, useContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Context consumed by inner blocks. | ||
*/ | ||
export type CartBlockControlsContextProps = { | ||
viewSwitcher: { | ||
component: () => JSX.Element | null; | ||
currentView: string; | ||
}; | ||
}; | ||
|
||
export const CartBlockControlsContext = createContext< | ||
CartBlockControlsContextProps | ||
>( { | ||
viewSwitcher: { | ||
component: () => null, | ||
currentView: 'filledCart', | ||
}, | ||
} ); | ||
|
||
export const useCartBlockControlsContext = (): CartBlockControlsContextProps => { | ||
return useContext( CartBlockControlsContext ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
assets/js/blocks/cart-checkout/cart-i2/use-view-switcher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useState } from '@wordpress/element'; | ||
import { Toolbar, ToolbarDropdownMenu } from '@wordpress/components'; | ||
import { BlockControls } from '@wordpress/block-editor'; | ||
import { Icon, eye } from '@woocommerce/icons'; | ||
|
||
interface View { | ||
view: string; | ||
label: string; | ||
icon: string | JSX.Element; | ||
default?: boolean; | ||
} | ||
|
||
export const useViewSwitcher = ( | ||
views: View[] | ||
): { | ||
currentView: string; | ||
component: () => JSX.Element; | ||
} => { | ||
const initialView = | ||
views?.find( ( view ) => view.default === true ) || views[ 0 ]; | ||
const [ currentView, setCurrentView ] = useState( initialView ); | ||
|
||
const ViewSwitcherComponent = () => ( | ||
<BlockControls> | ||
<Toolbar> | ||
<ToolbarDropdownMenu | ||
label={ __( | ||
'Switch view', | ||
'woo-gutenberg-products-block' | ||
) } | ||
text={ currentView.label } | ||
icon={ | ||
<Icon | ||
srcElement={ eye } | ||
style={ { marginRight: '8px' } } | ||
/> | ||
} | ||
controls={ views.map( ( view ) => ( { | ||
...view, | ||
title: view.label, | ||
onClick: () => setCurrentView( view ), | ||
} ) ) } | ||
/> | ||
</Toolbar> | ||
</BlockControls> | ||
); | ||
|
||
return { | ||
currentView: currentView.view, | ||
component: ViewSwitcherComponent, | ||
}; | ||
}; | ||
|
||
export default useViewSwitcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { SVG } from 'wordpress-components'; | ||
|
||
const eye = ( | ||
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
<path fill="none" d="M0 0h24v24H0V0z" /> | ||
<path d="M12 6a9.77 9.77 0 0 1 8.82 5.5C19.17 14.87 15.79 17 12 17s-7.17-2.13-8.82-5.5A9.77 9.77 0 0 1 12 6m0-2C7 4 2.73 7.11 1 11.5 2.73 15.89 7 19 12 19s9.27-3.11 11-7.5C21.27 7.11 17 4 12 4zm0 5a2.5 2.5 0 0 1 0 5 2.5 2.5 0 0 1 0-5m0-2c-2.48 0-4.5 2.02-4.5 4.5S9.52 16 12 16s4.5-2.02 4.5-4.5S14.48 7 12 7z" /> | ||
</SVG> | ||
); | ||
|
||
export default eye; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { SVG } from 'wordpress-components'; | ||
|
||
const filledCart = ( | ||
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
<g fill="none" fillRule="evenodd"> | ||
<path d="M0 0h24v24H0z" /> | ||
<path | ||
fill="currentColor" | ||
fillRule="nonzero" | ||
d="M15.55 13c.75 0 1.41-.41 1.75-1.03l3.58-6.49A.996.996 0 0 0 20.01 4H5.21l-.94-2H1v2h2l3.6 7.59-1.35 2.44C4.52 15.37 5.48 17 7 17h12v-2H7l1.1-2h7.45ZM7 18c-1.1 0-1.99.9-1.99 2S5.9 22 7 22s2-.9 2-2-.9-2-2-2Zm10 0c-1.1 0-1.99.9-1.99 2s.89 2 1.99 2 2-.9 2-2-.9-2-2-2Z" | ||
/> | ||
</g>{ ' ' } | ||
</SVG> | ||
); | ||
|
||
export default filledCart; |