-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3d7be6
commit 7b34ee8
Showing
7 changed files
with
173 additions
and
2 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
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,116 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React, { useState } from 'react'; | ||
import { useI18n } from '@automattic/react-i18n'; | ||
import { find, includes } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { I18n } from '@wordpress/i18n'; | ||
import { Button } from '@wordpress/components'; | ||
|
||
/** | ||
* Style dependencies | ||
*/ | ||
import './style.scss'; | ||
|
||
export type LanguageGroup = { | ||
id: string; | ||
name: ( translate: I18n[ '__' ] ) => string; | ||
subTerritories?: string[]; | ||
countries?: string[]; | ||
default?: boolean; | ||
}; | ||
|
||
type LanguageSlug = string; | ||
type WPLocale = string; | ||
|
||
type BaseLanguage = { | ||
langSlug: LanguageSlug; | ||
name: string; | ||
popular?: number; | ||
rtl?: boolean; | ||
territories: string[]; | ||
value: number; | ||
wpLocale: WPLocale | ''; | ||
}; | ||
|
||
type SubLanguage = BaseLanguage & { parentLangSlug: string }; | ||
|
||
export type Language = BaseLanguage | SubLanguage; | ||
|
||
type Props = { | ||
onSelectLanguage: ( language: Language ) => void; | ||
languages: Language[]; | ||
languageGroups: LanguageGroup[]; | ||
defaultLananguageGroupId: string; | ||
}; | ||
|
||
const LanguagePicker = ( { | ||
onSelectLanguage, | ||
languages, | ||
languageGroups, | ||
defaultLananguageGroupId, | ||
}: Props ) => { | ||
const { __ } = useI18n(); | ||
const [ filter, setFilter ] = useState< string >( defaultLananguageGroupId ); | ||
|
||
const getFilteredLanguages = () => { | ||
switch ( filter ) { | ||
case 'popular': | ||
return languages | ||
.filter( ( language: Language ) => language.popular ) | ||
.sort( | ||
( a: Language, b: Language ) => ( a.popular as number ) - ( b.popular as number ) | ||
); | ||
default: { | ||
const languageGroup = find( languageGroups, ( l: LanguageGroup ) => l.id === filter ); | ||
const subTerritories = languageGroup ? languageGroup.subTerritories : []; | ||
return languages | ||
.filter( ( language: Language ) => | ||
language.territories.some( ( t ) => includes( subTerritories, t ) ) | ||
) | ||
.sort( ( a: Language, b: Language ) => a.name.localeCompare( b.name ) ); | ||
} | ||
} | ||
}; | ||
|
||
const renderCategoryButtons = () => { | ||
return languageGroups.map( ( languageGroup ) => { | ||
const isSelected = filter === languageGroup.id; | ||
|
||
const onClick = () => { | ||
setFilter( languageGroup.id ); | ||
}; | ||
return ( | ||
<div key={ languageGroup.id }> | ||
<Button onClick={ onClick } className="language-picker__language-group"> | ||
<span className={ isSelected ? 'is-selected' : '' }>{ languageGroup.name( __ ) }</span> | ||
</Button> | ||
</div> | ||
); | ||
} ); | ||
}; | ||
|
||
return ( | ||
<div className="language-picker__content"> | ||
<div className="language-picker__category-filters">{ renderCategoryButtons() }</div> | ||
<div className="language-picker__language-buttons"> | ||
{ getFilteredLanguages().map( ( language: Language ) => ( | ||
<Button | ||
className="language-picker__language-button" | ||
key={ language.langSlug } | ||
onClick={ () => onSelectLanguage( language ) } | ||
title={ language.name } | ||
> | ||
<span lang={ language.langSlug }>{ language.name }</span> | ||
</Button> | ||
) ) } | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LanguagePicker; |
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,43 @@ | ||
@import '~@wordpress/base-styles/breakpoints'; | ||
|
||
.language-picker__language-group { | ||
width: 100%; | ||
} | ||
|
||
.language-picker__language-group > span { | ||
padding-bottom: 4px; | ||
&.is-selected { | ||
padding-bottom: 2px; | ||
border-bottom: 2px solid var( ---studio-blue-40 ); | ||
} | ||
} | ||
|
||
.language-picker__regions-label { | ||
margin: 6px 12px; | ||
color: var( --studio-gray-50 ); | ||
font-size: 13px; | ||
text-transform: uppercase; | ||
} | ||
|
||
.language-picker__page-content { | ||
display: flex; | ||
} | ||
|
||
.language-picker__category-filters { | ||
min-width: 170px; | ||
|
||
@include break-small { | ||
min-width: 220px; | ||
margin-right: 60px; | ||
} | ||
} | ||
|
||
.language-picker__language-buttons { | ||
display: flex; | ||
flex-wrap: wrap; | ||
align-content: flex-start; | ||
} | ||
|
||
.language-picker__language-item { | ||
width: 165px; | ||
} |
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