-
-
Notifications
You must be signed in to change notification settings - Fork 364
[Site] More Functional Code, 2x Complex Live Components Examples, few fixes #804
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
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
namespace Symfony\UX\TwigComponent\Twig; | ||
|
||
use Twig\Error\SyntaxError; | ||
|
||
/** | ||
* Rewrites <twig:component> syntaxes to {% component %} syntaxes. | ||
*/ | ||
|
@@ -46,12 +48,12 @@ public function preLexComponents(string $input): string | |
$this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock'] = false; | ||
} | ||
|
||
$output .= $this->consumeBlock(); | ||
$output .= $this->consumeBlock($componentName); | ||
|
||
continue; | ||
} | ||
|
||
$attributes = $this->consumeAttributes(); | ||
$attributes = $this->consumeAttributes($componentName); | ||
$isSelfClosing = $this->consume('/>'); | ||
if (!$isSelfClosing) { | ||
$this->consume('>'); | ||
|
@@ -79,7 +81,7 @@ public function preLexComponents(string $input): string | |
$lastComponentName = $lastComponent['name']; | ||
|
||
if ($closingComponentName !== $lastComponentName) { | ||
throw new \RuntimeException("Expected closing tag '</twig:{$lastComponentName}>' but found '</twig:{$closingComponentName}>' at line {$this->line}"); | ||
throw new SyntaxError("Expected closing tag '</twig:{$lastComponentName}>' but found '</twig:{$closingComponentName}>'", $this->line); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a correction: allows the lexing error to show the template + line number in the exception. |
||
} | ||
|
||
// we've reached the end of this component. If we're inside the | ||
|
@@ -112,28 +114,40 @@ public function preLexComponents(string $input): string | |
|
||
if (!empty($this->currentComponents)) { | ||
$lastComponent = array_pop($this->currentComponents)['name']; | ||
throw new \RuntimeException(sprintf('Expected closing tag "</twig:%s>" not found at line %d.', $lastComponent, $this->line)); | ||
throw new SyntaxError(sprintf('Expected closing tag "</twig:%s>" not found.', $lastComponent), $this->line); | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
private function consumeComponentName(): string | ||
private function consumeComponentName(string $customExceptionMessage = null): string | ||
{ | ||
$start = $this->position; | ||
while ($this->position < $this->length && preg_match('/[A-Za-z0-9_:@\-\/.]/', $this->input[$this->position])) { | ||
++$this->position; | ||
} | ||
|
||
$componentName = substr($this->input, $start, $this->position - $start); | ||
|
||
if (empty($componentName)) { | ||
throw new \RuntimeException("Expected component name at line {$this->line}"); | ||
$exceptionMessage = $customExceptionMessage; | ||
if (null == $exceptionMessage) { | ||
$exceptionMessage = 'Expected component name when resolving the "<twig:" syntax.'; | ||
} | ||
throw new SyntaxError($exceptionMessage, $this->line); | ||
} | ||
|
||
return $componentName; | ||
} | ||
|
||
private function consumeAttributes(): string | ||
private function consumeAttributeName(string $componentName): string | ||
{ | ||
$message = sprintf('Expected attribute name when parsing the "<twig:%s" syntax.', $componentName); | ||
|
||
return $this->consumeComponentName($message); | ||
} | ||
|
||
private function consumeAttributes(string $componentName): string | ||
{ | ||
$attributes = []; | ||
|
||
|
@@ -151,7 +165,7 @@ private function consumeAttributes(): string | |
$isAttributeDynamic = true; | ||
} | ||
|
||
$key = $this->consumeComponentName(); | ||
$key = $this->consumeAttributeName($componentName); | ||
|
||
// <twig:component someProp> -> someProp: true | ||
if (!$this->check('=')) { | ||
|
@@ -202,13 +216,13 @@ private function consume(string $string): bool | |
private function consumeChar($validChars = null): string | ||
{ | ||
if ($this->position >= $this->length) { | ||
throw new \RuntimeException('Unexpected end of input'); | ||
throw new SyntaxError('Unexpected end of input', $this->line); | ||
} | ||
|
||
$char = $this->input[$this->position]; | ||
|
||
if (null !== $validChars && !\in_array($char, (array) $validChars, true)) { | ||
throw new \RuntimeException('Expected one of ['.implode('', (array) $validChars)."] but found '{$char}' at line {$this->line}"); | ||
throw new SyntaxError('Expected one of ['.implode('', (array) $validChars)."] but found '{$char}'.", $this->line); | ||
} | ||
|
||
++$this->position; | ||
|
@@ -255,7 +269,7 @@ private function expectAndConsumeChar(string $char): void | |
} | ||
|
||
if ($this->position >= $this->length || $this->input[$this->position] !== $char) { | ||
throw new \RuntimeException("Expected '{$char}' but found '{$this->input[$this->position]}' at line {$this->line}"); | ||
throw new SyntaxError("Expected '{$char}' but found '{$this->input[$this->position]}'.", $this->line); | ||
} | ||
++$this->position; | ||
} | ||
|
@@ -276,9 +290,9 @@ private function check(string $chars): bool | |
return true; | ||
} | ||
|
||
private function consumeBlock(): string | ||
private function consumeBlock(string $componentName): string | ||
{ | ||
$attributes = $this->consumeAttributes(); | ||
$attributes = $this->consumeAttributes($componentName); | ||
$this->consume('>'); | ||
|
||
$blockName = ''; | ||
|
@@ -291,14 +305,14 @@ private function consumeBlock(): string | |
} | ||
|
||
if (empty($blockName)) { | ||
throw new \RuntimeException("Expected block name at line {$this->line}"); | ||
throw new SyntaxError('Expected block name.', $this->line); | ||
} | ||
|
||
$output = "{% block {$blockName} %}"; | ||
|
||
$closingTag = '</twig:block>'; | ||
if (!$this->doesStringEventuallyExist($closingTag)) { | ||
throw new \RuntimeException("Expected closing tag '{$closingTag}' for block '{$blockName}' at line {$this->line}"); | ||
throw new SyntaxError("Expected closing tag '{$closingTag}' for block '{$blockName}'.", $this->line); | ||
} | ||
$blockContents = $this->consumeUntil($closingTag); | ||
|
||
|
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
20 changes: 20 additions & 0 deletions
20
ux.symfony.com/assets/controllers/bootstrap-modal-controller.js
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,20 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
import Modal from 'bootstrap/js/dist/modal'; | ||
|
||
/** | ||
* Allows you to dispatch a "modal:close" JavaScript event to close it. | ||
* | ||
* This is useful inside a LiveComponent, where you can emit a browser event | ||
* to open or close the modal. | ||
* | ||
* See templates/components/BootstrapModal.html.twig to see how this is | ||
* attached to Bootstrap modal. | ||
*/ | ||
export default class extends Controller { | ||
modal = null; | ||
|
||
connect() { | ||
this.modal = Modal.getOrCreateInstance(this.element); | ||
document.addEventListener('modal:close', () => this.modal.hide()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
ux.symfony.com/assets/controllers/code-expander-controller.js
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,28 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
export default class extends Controller { | ||
static targets = ['useStatements', 'expandCodeButton', 'codeContent']; | ||
|
||
connect() { | ||
if (this.#isOverflowing(this.codeContentTarget)) { | ||
this.expandCodeButtonTarget.style.display = 'block'; | ||
// add extra padding so the button doesn't block the code | ||
this.codeContentTarget.classList.add('pb-5'); | ||
} | ||
} | ||
|
||
expandUseStatements(event) { | ||
this.useStatementsTarget.style.display = 'block'; | ||
event.currentTarget.remove(); | ||
} | ||
|
||
expandCode(event) { | ||
this.codeContentTarget.style.height = 'auto'; | ||
this.codeContentTarget.classList.remove('pb-5'); | ||
this.expandCodeButtonTarget.remove(); | ||
} | ||
|
||
#isOverflowing(element) { | ||
return element.scrollHeight > element.clientHeight; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
ux.symfony.com/assets/controllers/ux-clipboard-controller.js
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,20 @@ | ||
import Clipboard from 'stimulus-clipboard' | ||
|
||
export default class extends Clipboard { | ||
static values = { | ||
source: String, | ||
} | ||
|
||
/** | ||
* Overridden so we can simply pass the value in via a value. | ||
*/ | ||
copy(event) { | ||
if (!this.sourceValue) { | ||
super.copy(event); | ||
} | ||
|
||
event.preventDefault(); | ||
|
||
navigator.clipboard.writeText(this.sourceValue).then(() => this.copied()); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I snuck this feature in. We may need some trial and error. But throwing a
RuntimeError
allows Twig to show the actual template + line number where the error occurred. The nested exception will be the one from your component (e.g. maybe you are passingnull
to a property in your component that only acceptsstring
).