Skip to content

Commit

Permalink
Added basic subcomponent toggle highlighting in the email sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
crismali committed Dec 13, 2023
1 parent 5268b39 commit eba50df
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/templates/CurrentlyActiveEmailPart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ export const CurrentlyActiveEmailPartContext = createContext<CurrentlyActiveEmai
() => {},
])

export const CurrentlyActiveEmailPart: FC<{ children: ReactNode }> = ({ children }) => {
const value = useState('')
interface CurrentlyActiveEmailPartProps {
children: ReactNode
initiallyActiveEmailPartId?: string
}

export const CurrentlyActiveEmailPart: FC<CurrentlyActiveEmailPartProps> = ({
children,
initiallyActiveEmailPartId,
}) => {
const value = useState(initiallyActiveEmailPartId ?? '')

return (
<CurrentlyActiveEmailPartContext.Provider value={value}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
margin: 1rem 0;
padding: 0 2rem;
}

.accordion-email-subcomponent:first-of-type {
margin-top: 1.5rem;
}
Expand All @@ -114,6 +115,14 @@
align-items: center;
display: flex;
justify-content: space-between;
border-radius: 5px;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
transition: background-color ease-in-out 150ms;
}

.accordion-email-subcomponent .label-and-toggle.active {
background-color: var(--gray-medium);
}

.accordion-email-subcomponent label {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import './EmailEditorSidebarAccordion.css'
import { RightPointer } from 'src/ui/RightPointer'
import { EmailSubComponentControls } from './EmailSubcomponentControls'
import { labelForComponent } from './labelForComponent'
import { useIsCurrentlyActiveEmailPart } from '../CurrentlyActiveEmailPart'
import classNames from 'classnames'

interface ContainerProps {
children: ReactNode
Expand Down Expand Up @@ -97,9 +99,11 @@ interface EmailSubComponentProps {
const EmailSubComponent: FC<EmailSubComponentProps> = ({ componentId, emailSubComponent }) => {
const toggleId = `toggle-${emailSubComponent.id}`
const shouldShow = useShouldShowEmailPart(emailSubComponent.id)
const { isActive } = useIsCurrentlyActiveEmailPart(emailSubComponent.id)

return (
<div className="accordion-email-subcomponent">
<div className="label-and-toggle">
<div className={classNames('label-and-toggle', { active: isActive })}>
<label htmlFor={toggleId}>{labelForSubComponent(emailSubComponent.kind)}</label>
<Toggle
id={toggleId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { EmailEditorSidebarAccordion } from '../EmailEditorSidebarAccordion'
import { EmailTemplate } from 'src/appTypes'
import {
WrapperComponent,
buildEmailTemplateSubComponent,
buildUniqueEmailComponent,
buildUniqueEmailSubComponent,
} from 'src/testHelpers'
import { CurrentlyActiveEmailPart } from 'src/templates/CurrentlyActiveEmailPart'

describe(EmailEditorSidebarAccordion.Container.displayName!, () => {
it('displays its children', () => {
Expand Down Expand Up @@ -342,4 +342,32 @@ describe(EmailEditorSidebarAccordion.EmailSubComponent.displayName!, () => {
)
expect(queryByText('Status variant')).not.toBeNull()
})

it('is highlighted when the subcomponent is active', () => {
const { baseElement } = render(
<ShouldShowEmailPart>
<CurrentlyActiveEmailPart initiallyActiveEmailPartId={emailSubComponent.id}>
<EmailEditorSidebarAccordion.EmailSubComponent
componentId={faker.lorem.word()}
emailSubComponent={emailSubComponent}
/>
</CurrentlyActiveEmailPart>
</ShouldShowEmailPart>,
)
expect(baseElement.querySelector('.active')).not.toBeNull()
})

it('is not hightlighted when the subcomponent is inactive', () => {
const { baseElement } = render(
<ShouldShowEmailPart>
<CurrentlyActiveEmailPart initiallyActiveEmailPartId={faker.lorem.words(3)}>
<EmailEditorSidebarAccordion.EmailSubComponent
componentId={faker.lorem.word()}
emailSubComponent={emailSubComponent}
/>
</CurrentlyActiveEmailPart>
</ShouldShowEmailPart>,
)
expect(baseElement.querySelector('.active')).toBeNull()
})
})

0 comments on commit eba50df

Please sign in to comment.