- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6
Toggle password #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Toggle password #206
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3eb9992
              
                Replace deprecated ux-toggle-password with own form type
              
              
                absumo 56d377d
              
                Enable by default on all (repeated)password fields
              
              
                absumo f706b7c
              
                Add docs for password toggle
              
              
                absumo 506547d
              
                Add translations for toggle password
              
              
                absumo 4989446
              
                Move toggle password button to bottom on login page
              
              
                 edd193f
              
                Customizing icon is not needed
              
              
                absumo 1b1916a
              
                Use more specific labels
              
              
                absumo 093fcbf
              
                Allow keyboard navigation
              
              
                absumo fcda9e5
              
                Fix code example
              
              
                absumo 014539f
              
                Fix toggle password service id
              
              
                absumo 60ed1ac
              
                Translator should not be null
              
              
                absumo File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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,55 @@ | ||
| import { Controller } from '@hotwired/stimulus' | ||
|  | ||
| export default class extends Controller { | ||
| static values = { | ||
| visibleLabel: { type: String, default: 'Show password' }, | ||
| hiddenLabel: { type: String, default: 'Hide password' }, | ||
| buttonClasses: Array | ||
| } | ||
|  | ||
| isDisplayed = false | ||
| visibleIcon = `<svg xmlns="http://www.w3.org/2000/svg" class="toggle-password-icon" viewBox="0 0 20 20" fill="currentColor"> | ||
| <path d="M10 12a2 2 0 100-4 2 2 0 000 4z" /> | ||
| <path fill-rule="evenodd" d="M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z" clip-rule="evenodd" /> | ||
| </svg>` | ||
| hiddenIcon = `<svg xmlns="http://www.w3.org/2000/svg" class="toggle-password-icon" viewBox="0 0 20 20" fill="currentColor"> | ||
| <path fill-rule="evenodd" d="M3.707 2.293a1 1 0 00-1.414 1.414l14 14a1 1 0 001.414-1.414l-1.473-1.473A10.014 10.014 0 0019.542 10C18.268 5.943 14.478 3 10 3a9.958 9.958 0 00-4.512 1.074l-1.78-1.781zm4.261 4.26l1.514 1.515a2.003 2.003 0 012.45 2.45l1.514 1.514a4 4 0 00-5.478-5.478z" clip-rule="evenodd" /> | ||
| <path d="M12.454 16.697L9.75 13.992a4 4 0 01-3.742-3.741L2.335 6.578A9.98 9.98 0 00.458 10c1.274 4.057 5.065 7 9.542 7 .847 0 1.669-.105 2.454-.303z" /> | ||
| </svg>` | ||
|  | ||
| connect () { | ||
| const button = this.createButton() | ||
|  | ||
| this.element.insertAdjacentElement('afterend', button) | ||
| this.dispatchEvent('connect', { element: this.element, button }) | ||
| } | ||
|  | ||
| /** | ||
| * @returns {HTMLButtonElement} | ||
| */ | ||
| createButton () { | ||
| const button = document.createElement('button') | ||
| button.type = 'button' | ||
| button.classList.add(...this.buttonClassesValue) | ||
| button.addEventListener('click', this.toggle.bind(this)) | ||
| button.innerHTML = `${this.visibleIcon} ${this.visibleLabelValue}` | ||
|         
                  tijsverkoyen marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| return button | ||
| } | ||
|  | ||
| /** | ||
| * Toggle input type between "text" or "password" and update label accordingly | ||
| */ | ||
| toggle (event) { | ||
| this.isDisplayed = !this.isDisplayed | ||
| const toggleButtonElement = event.currentTarget | ||
| toggleButtonElement.innerHTML = this.isDisplayed | ||
| ? `${this.hiddenIcon} ${this.hiddenLabelValue}` | ||
| : `${this.visibleIcon} ${this.visibleLabelValue}` | ||
|         
                  tijsverkoyen marked this conversation as resolved.
              Show resolved
            Hide resolved         
                  tijsverkoyen marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| this.element.setAttribute('type', this.isDisplayed ? 'text' : 'password') | ||
| this.dispatchEvent(this.isDisplayed ? 'show' : 'hide', { element: this.element, button: toggleButtonElement }) | ||
| } | ||
|  | ||
| dispatchEvent (name, payload) { | ||
| this.dispatch(name, { detail: payload, prefix: 'toggle-password' }) | ||
| } | ||
| } | ||
  
    
      This file contains hidden or 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 hidden or 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,30 @@ | ||
| .toggle-password-container { | ||
| position: relative; | ||
| } | ||
|  | ||
| .toggle-password-icon { | ||
| height: 1rem; | ||
| width: 1rem; | ||
| } | ||
|  | ||
| .toggle-password-button { | ||
| place-items: center center; | ||
| background-color: transparent; | ||
| border: none; | ||
| column-gap: 0.25rem; | ||
| display: flex; | ||
| flex-direction: row; | ||
| font-size: 0.875rem; | ||
| height: 1rem; | ||
| line-height: 1.25rem; | ||
| position: absolute; | ||
| right: 0.5rem; | ||
| top: -1.25rem; | ||
| } | ||
|  | ||
| .user { | ||
| .toggle-password-button { | ||
| top: unset; | ||
| bottom: -1.25rem; | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,82 @@ | ||
| <?php | ||
|  | ||
| namespace SumoCoders\FrameworkCoreBundle\Form\Extension; | ||
|  | ||
| use Symfony\Component\Form\AbstractTypeExtension; | ||
| use Symfony\Component\Form\Extension\Core\Type\PasswordType; | ||
| use Symfony\Component\Form\FormInterface; | ||
| use Symfony\Component\Form\FormView; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
| use Symfony\Component\Translation\TranslatableMessage; | ||
| use Symfony\Contracts\Translation\TranslatorInterface; | ||
|  | ||
| final class TogglePasswordTypeExtension extends AbstractTypeExtension | ||
| { | ||
| public function __construct(private readonly TranslatorInterface $translator) | ||
| { | ||
| } | ||
|  | ||
| public static function getExtendedTypes(): iterable | ||
| { | ||
| return [PasswordType::class]; | ||
| } | ||
|  | ||
| public function configureOptions(OptionsResolver $resolver): void | ||
| { | ||
| $resolver->setDefaults([ | ||
| 'toggle' => true, | ||
| 'hidden_label' => 'Hide password', | ||
| 'visible_label' => 'Show password', | ||
| 'button_classes' => ['toggle-password-button'], | ||
| 'toggle_container_classes' => ['toggle-password-container'], | ||
| 'use_toggle_form_theme' => true, | ||
| ]); | ||
|  | ||
| $resolver->setAllowedTypes('toggle', ['bool']); | ||
| $resolver->setAllowedTypes('hidden_label', ['string', TranslatableMessage::class, 'null']); | ||
| $resolver->setAllowedTypes('visible_label', ['string', TranslatableMessage::class, 'null']); | ||
| $resolver->setAllowedTypes('button_classes', ['string[]']); | ||
| $resolver->setAllowedTypes('toggle_container_classes', ['string[]']); | ||
| $resolver->setAllowedTypes('use_toggle_form_theme', ['bool']); | ||
| } | ||
|  | ||
| public function buildView(FormView $view, FormInterface $form, array $options): void | ||
| { | ||
| $view->vars['toggle'] = $options['toggle']; | ||
|  | ||
| if (!$options['toggle']) { | ||
| return; | ||
| } | ||
|  | ||
| if ($options['use_toggle_form_theme']) { | ||
| array_splice($view->vars['block_prefixes'], -1, 0, 'toggle_password'); | ||
| } | ||
|  | ||
| $controllerName = 'toggle-password'; | ||
| $view->vars['attr']['data-controller'] = trim(\sprintf('%s %s', $view->vars['attr']['data-controller'] ?? '', $controllerName)); | ||
|  | ||
| $controllerValues['hidden-label'] = $this->translateLabel($options['hidden_label'], $options['translation_domain']); | ||
| $controllerValues['visible-label'] = $this->translateLabel($options['visible_label'], $options['translation_domain']); | ||
|  | ||
| $controllerValues['button-classes'] = json_encode($options['button_classes'], \JSON_THROW_ON_ERROR); | ||
|  | ||
| foreach ($controllerValues as $name => $value) { | ||
| $view->vars['attr'][\sprintf('data-%s-%s-value', $controllerName, $name)] = $value; | ||
| } | ||
|  | ||
| $view->vars['toggle_container_classes'] = $options['toggle_container_classes']; | ||
| } | ||
|  | ||
| private function translateLabel(string|TranslatableMessage|null $label, ?string $translationDomain): ?string | ||
| { | ||
| if (null === $label) { | ||
| return $label; | ||
| } | ||
|  | ||
| if ($label instanceof TranslatableMessage) { | ||
| return $label->trans($this->translator); | ||
| } | ||
|  | ||
| return $this->translator->trans($label, domain: $translationDomain); | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.