Skip to content

Instantly share code, notes, and snippets.

@mcpar-land
Created July 14, 2020 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcpar-land/0295739f72929a8f3d9b73d05467615f to your computer and use it in GitHub Desktop.
Save mcpar-land/0295739f72929a8f3d9b73d05467615f to your computer and use it in GitHub Desktop.
React subcomponents with styled-components and typescript
import React from 'react'
import styled from 'styled-components'
const MenuBase = styled.div`
display: flex;
flex-direction: column;
`
const MenuItem = styled.div`
padding: 5px;
`
const MenuSubMenu = styled(MenuBase)`
margin-left: 10px;
`
const Menu: typeof MenuBase & {
Item: typeof MenuItem
Menu: typeof MenuSubMenu
} = Object.assign(MenuBase, {
Item: MenuItem,
Menu: MenuSubMenu,
})
const example = (
<Menu>
<Menu.Item>This is a menu item.</Menu.Item>
<Menu.Item>This is another menu item.</Menu.Item>
<Menu.Menu>
<Menu.Item>This is a submenu item.</Menu.Item>
<Menu.Item>This is another submenu item.</Menu.Item>
</Menu.Menu>
</Menu>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment