Skip to content

Commit

Permalink
try using role button
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpatrickpatrick committed Jan 15, 2025
1 parent d52283f commit cf6cfad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,21 @@
margin-left: govuk-spacing(2);
}
}

.govuk-file-upload:focus + .govuk-button {
background-color: $govuk-focus-colour;
}

.govuk-file-upload:disabled + .govuk-button {
opacity: (0.5);

&:hover {
background-color: govuk-colour("light-grey");
cursor: not-allowed;
}

&:active {
top: 0;
box-shadow: 0 $govuk-border-width-form-element 0 govuk-shade(govuk-colour("white"), 60%); // s0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@ export class FileUpload extends ConfigurableComponent {
$wrapper.className = 'govuk-file-upload-wrapper'

// Create the file selection button
const $button = document.createElement('button')
const $button = document.createElement('span')
$button.className =
'govuk-button govuk-button--secondary govuk-file-upload__button'
$button.type = 'button'
$button.innerText = this.i18n.t('selectFilesButton')
$button.setAttribute('role', 'button')
$button.setAttribute('aria-hidden', 'true')
$button.addEventListener('click', this.onClick.bind(this))

// Create status element that shows what/how many files are selected
const $status = document.createElement('span')
$status.className = 'govuk-body govuk-file-upload__status'
$status.innerText = this.i18n.t('filesSelectedDefault')
$status.setAttribute('role', 'status')
$status.setAttribute('aria-hidden', 'true')

// Assemble these all together
$wrapper.insertAdjacentElement('beforeend', $button)
Expand All @@ -85,13 +86,6 @@ export class FileUpload extends ConfigurableComponent {
this.$button = $button
this.$status = $status

// Prevent the hidden input being tabbed to by keyboard users
this.$root.setAttribute('tabindex', '-1')

// Syncronise the `disabled` state between the button and underlying input
this.updateDisabledState()
this.observeDisabledState()

// Bind change event to the underlying input
this.$root.addEventListener('change', this.onChange.bind(this))

Expand Down Expand Up @@ -175,34 +169,6 @@ export class FileUpload extends ConfigurableComponent {
this.$wrapper.classList.remove('govuk-file-upload-wrapper--show-dropzone')
}

/**
* Create a mutation observer to check if the input's attributes altered.
*/
observeDisabledState() {
const observer = new MutationObserver((mutationList) => {
for (const mutation of mutationList) {
console.log('mutation', mutation)
if (
mutation.type === 'attributes' &&
mutation.attributeName === 'disabled'
) {
this.updateDisabledState()
}
}
})

observer.observe(this.$root, {
attributes: true
})
}

/**
* Synchronise the `disabled` state between the input and replacement button.
*/
updateDisabledState() {
this.$button.disabled = this.$root.disabled
}

/**
* Name for the component used when initialising using data-module attributes.
*/
Expand Down

0 comments on commit cf6cfad

Please sign in to comment.